Friday, April 27, 2012

Apply functions to subsets of your data



This video explains two ways to apply functions to subsets of the data, with illustration using histograms and our example dataset / syntax, which has been updated to include this syntax, which is also copied below.




par(mfrow=c(2,3))


##   select cases to include by specifying value of factor variable

annie<-hist(AllVars$NewConf1,
col="purple",
breaks=4,
ylim=c(0,12))


ed<-hist(AllVars$NewConf1 [AllVars$TwoPlusComputer == 1],
col="light blue",
breaks=4,
ylim=c(0,12))


frankie<-hist(AllVars$NewConf1 [AllVars$TwoPlusComputer == 0],
col="pink",
breaks=4,
ylim=c(0,12))

##  select cases by refering to different datasets
## to do this you need to first use the
# AllVars<-data.frame(cbind(var, var2, varn)
# then do the subset command to make a new dataset
#  NewDataSetName<- subset(AllVars, Variable == 1)

frannie<-hist(AllVars$NewConf1,
col="purple",
breaks=4,
ylim=c(0,12))

fred<-hist(Geek$NewConf1,
col="blue",
breaks=4,
ylim=c(0,12))

frank<-hist(NonGeek$NewConf1,
col="red",
breaks=4,
ylim=c(0,12))

No comments:

Post a Comment