A Singh
2009-Dec-08 14:56 UTC
[R] Printing 'k' levels of factors 'n' times each, but 'n' is unequal for all levels ?
Dear List, I need to print out each of 'k' levels of a factor 'n' times each, where 'n' is the number of elements belonging to each factor. I know that this can normally be done using the gl() command, but in my case, each level 'k' has an unequal number of elements. Example with code is as below: vc<-read.table("P:\\Transit\\CORRECT files\\Everything-newest.csv",header=T, sep=",", dec=".", na.strings=NA, strip.white=T) vcdf<-data.frame(vc) tempdf<-data.frame(cbind(vcdf[,1:3], vcdf[,429])) newtemp<-na.exclude(tempdf) newtemp[,2]<-factor(newtemp[,2]) groupmean<-tapply(newtemp[,4], newtemp[,2], mean) newmark<-factor(groupmean, exclude=(groupmean==0 | groupmean==1)) newmark This is what the output is (going up to 61 levels) 1 2 3 4 <NA> 0.142857142857143 0.444444444444444 <NA> 5 6 8 9 0.3333333333 0.09090909090 0.3846153846 <NA> ............. 61 <NA> The variable 'groupmean' calculates means for newtemp[,4] for 61 levels (k). Levels are specified in newtemp[,2]. I now want to be able to print out each value of 'groupmean' as many times as there are elements in the group for which each is calculated. So for E.g. if level 1 of newtemp[,2] has about 15 elements, <NA> should be printed 15 times, level 2 = 12 times 0.1428, and so on. Is there a way of specifying that a list needs to be populated with replicates of groupmeans based on values got from newtemp[,2]? I just can't seem to figure this out by myself. Many thanks for your help. Aditi ---------------------- A Singh Aditi.Singh at bristol.ac.uk School of Biological Sciences University of Bristol
Uwe Ligges
2009-Dec-09 15:44 UTC
[R] Printing 'k' levels of factors 'n' times each, but 'n' is unequal for all levels ?
A Singh wrote:> Dear List, > > I need to print out each of 'k' levels of a factor 'n' times each, where > 'n' is the number of elements belonging to each factor. > > I know that this can normally be done using the gl() command, > but in my case, each level 'k' has an unequal number of elements. > > Example with code is as below: > > vc<-read.table("P:\\Transit\\CORRECT > files\\Everything-newest.csv",header=T, sep=",", dec=".", na.strings=NA, > strip.white=T) > > vcdf<-data.frame(vc) > > tempdf<-data.frame(cbind(vcdf[,1:3], vcdf[,429])) > newtemp<-na.exclude(tempdf) > > newtemp[,2]<-factor(newtemp[,2]) > > groupmean<-tapply(newtemp[,4], newtemp[,2], mean) > > newmark<-factor(groupmean, exclude=(groupmean==0 | groupmean==1)) > newmark > > This is what the output is (going up to 61 levels) > 1 2 3 4 > <NA> 0.142857142857143 0.444444444444444 <NA> > 5 6 8 9 > 0.3333333333 0.09090909090 0.3846153846 <NA> > > ............. 61 > <NA> > > The variable 'groupmean' calculates means for newtemp[,4] for 61 levels > (k). Levels are specified in newtemp[,2]. > > I now want to be able to print out each value of 'groupmean' as many > times as there are elements in the group for which each is calculated. > > So for E.g. if level 1 of newtemp[,2] has about 15 elements, <NA> should > be printed 15 times, level 2 = 12 times 0.1428, and so on. > > Is there a way of specifying that a list needs to be populated with > replicates of groupmeans based on values got from newtemp[,2]?See ?mapply and ?rep, hence mapply(rep, values, replicates) where "values" and "replicates" are corresponding vectors. Uwe Ligges> I just can't seem to figure this out by myself. > > Many thanks for your help. > > Aditi > > ---------------------- > A Singh > Aditi.Singh at bristol.ac.uk > School of Biological Sciences > University of Bristol > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.