#einlesen
mydata <- read.table("C:\\R\\mydata.txt",
 header=TRUE,sep=",",row.names="id")

#ausgabe
print(mydata)

#summary statistic mit min,max,avg, median
summary(mydata["q1"])

#variablennamen
names(mydata)

#subset ausw
summary( subset(mydata,select=q1:q4) )

#1 attribut einer variable
print(mydata[ mydata$gender=="f", ])

#transformation
mydata <- transform( mydata,
sum=q1+q2+q3+q4,
mean=sum/4)

print(mydata)

# The new variable q4SAgree will be 1 if q4 equals 5, otherwise zero.
# It identifies the people who strongly agree with question 4.
attach(mydata)
mydata$q4Sagree <- ifelse( q4 == 5, 1,0 )
print(mydata)
detach(mydata)

#This uses the ifelse function on the whole of q1 at once.
attach(mydata)
mydata$q1 <- ifelse( q1 == 5, 8, mydata$q1)
print(mydata)
detach(mydata)

#variablen umbenennen

install.packages("reshape")
library(reshape)
mydata <- rename(mydata, c(q1="x1")) #Note the new name is one in
quotes.
mydata <- rename(mydata, c(q2="x2"))
mydata <- rename(mydata, c(q3="x3"))
mydata <- rename(mydata, c(q4="x4"))
print(mydata)

#recoding
#achtung geht derzeit nicht
install.packages("cars")
attach(mydata)
library(cars)
mydata$q1<-recode(q1,"1=2;5=4")
mydata$q2<-recode(q2,"1=2;5=4")
mydata$q3<-recode(q3,"1=2;5=4")
mydata$q4<-recode(q4,"1=2;5=4")
mydata

#datei aufteilen
attach(mydata)
by(mydata,gender,summary)

attach(mydata)
males <- mydata[gender=="m", ]
print(males)

females <- mydata[gender=="f", ]
print(females)

both<-rbind(females,males)
print(both)

#sortieren
# Now sort by gender then workshop:
mydataSorted<-mydata[ order( mydata$gender, mydata$workshop ), ]
print(mydataSorted)
# The default sort order is ascending. Prefix any variable
# with a minus sign to get descending order.
mydataSorted<- mydata[ order( -mydata$workshop,mydata$gender ), ]
print(mydataSorted)

#graphics
#Basic Graphics in R.
attach(mydata) #Makes this the default dataset.
hist(q1)
barplot(c(40,60))
barplot(summary(gender))

# Scatterplot of q1 by q2.
plot(q1,q2)
# Scatterplot matrix of whole data frame.
plot(mydata)

#statistical tests
install.packages("foreign")
install.packages("Hmisc")
install.packages("prettyR")

library(foreign)
library(Hmisc)
library(prettyR)


describe(mydata)