Script 2
# Exercise
#.1 Give each 10th element of mis_arith1 NA
mis_arith1 <- arith1
mis_arith1[seq(1,150,10)] <- NA
#.2 Add NA to every 17th variable of mis_educ and mis_educf
mis_educ <- educ
mis_educ[seq(1,150,17)] <- NA
mis_educf <- educf
mis_educf[seq(1,150,17)] <- NA
#.3 Add NA to mis_IQ < 85
mis_IQ <- IQ
mis_IQ <- ifelse(mis_IQ < 85, NA, IQ)
#4. Add NA to mis_arith0 if arith0 > 140
mis_arith0 <- ifelse(mis_arith0>140, NA, mis_arith0)
??ifelse
#5. Make indicators for missing elements
ind_educ <- as.numeric(is.na(mis_educ))
ind_IQ <- as.numeric(is.na(mis_IQ))
ind_arith0 <- as.numeric(is.na(mis_arith0))
ind_arith1 <- as.numeric(is.na(mis_arith1))
#6. Make data frame
misdata <- data.frame(ID, method, methodf, sex, sexf, mis_IQ, mis_arith0, mis_arith1, mis_educ, mis_educf, ind_educ, ind_IQ, ind_arith0, ind_arith1)
#7. Give overviews and compare
summary(misdata)
summary(mydata)
#8. Make complete dataframe without the NAs
compdata <- na.omit(misdata)
#9. Give overview of compdata
View(compdata)
#10. Save data frames
save(misdata, file="misdata.Rdata")
save(compdata, file="compdata.Rdata")
#1. load library
library(foreign)
#2. Read data file
worms <- read.spss("C:/Users/Lenovo/Dropbox/Studie/Masters/Blok 1a/R workshop/Worms.sav", to.dataframe="True")
#3. Inspect file
summary(worms)
edit(worms)
name(worms)
# Field.name, Vegetation and Damp are factor, others are numeric
#4. Plot soil against worm density
plot(worms$Soil.pH, worms$Worm.density)
#5. Add straight lines
abline(mean(worms$Worm.density),0)
abline(lm(worms$Worm.density ~ worms$Soil.pH))
#6. Split area by vegetation
areabyveg <- split(worms$Area, worms$Vegetation, drop = "FALSE", sep=".", lex.order="FALSE"); areabyveg
sapply(areabyveg,length)
sapply(areabyveg,mean)
boxplot(areabyveg, col="lavender", notch = FALSE, varwidth = TRUE)
# I've inserted notch = "FALSE" because TRUE gave an error but I'm not sure what this function does
#7. Tidy up
rm(areabyveg)
#.1 Give each 10th element of mis_arith1 NA
mis_arith1 <- arith1
mis_arith1[seq(1,150,10)] <- NA
#.2 Add NA to every 17th variable of mis_educ and mis_educf
mis_educ <- educ
mis_educ[seq(1,150,17)] <- NA
mis_educf <- educf
mis_educf[seq(1,150,17)] <- NA
#.3 Add NA to mis_IQ < 85
mis_IQ <- IQ
mis_IQ <- ifelse(mis_IQ < 85, NA, IQ)
#4. Add NA to mis_arith0 if arith0 > 140
mis_arith0 <- ifelse(mis_arith0>140, NA, mis_arith0)
??ifelse
#5. Make indicators for missing elements
ind_educ <- as.numeric(is.na(mis_educ))
ind_IQ <- as.numeric(is.na(mis_IQ))
ind_arith0 <- as.numeric(is.na(mis_arith0))
ind_arith1 <- as.numeric(is.na(mis_arith1))
#6. Make data frame
misdata <- data.frame(ID, method, methodf, sex, sexf, mis_IQ, mis_arith0, mis_arith1, mis_educ, mis_educf, ind_educ, ind_IQ, ind_arith0, ind_arith1)
#7. Give overviews and compare
summary(misdata)
summary(mydata)
#8. Make complete dataframe without the NAs
compdata <- na.omit(misdata)
#9. Give overview of compdata
View(compdata)
#10. Save data frames
save(misdata, file="misdata.Rdata")
save(compdata, file="compdata.Rdata")
#######
# Assignment#1. load library
library(foreign)
#2. Read data file
worms <- read.spss("C:/Users/Lenovo/Dropbox/Studie/Masters/Blok 1a/R workshop/Worms.sav", to.dataframe="True")
#3. Inspect file
summary(worms)
edit(worms)
name(worms)
# Field.name, Vegetation and Damp are factor, others are numeric
#4. Plot soil against worm density
plot(worms$Soil.pH, worms$Worm.density)
#5. Add straight lines
abline(mean(worms$Worm.density),0)
abline(lm(worms$Worm.density ~ worms$Soil.pH))
#6. Split area by vegetation
areabyveg <- split(worms$Area, worms$Vegetation, drop = "FALSE", sep=".", lex.order="FALSE"); areabyveg
sapply(areabyveg,length)
sapply(areabyveg,mean)
boxplot(areabyveg, col="lavender", notch = FALSE, varwidth = TRUE)
# I've inserted notch = "FALSE" because TRUE gave an error but I'm not sure what this function does
#7. Tidy up
rm(areabyveg)
Reacties
Een reactie posten