[R] Printing 'k' levels of factors 'n' times each, but 'n' is unequal for all levels ?
ligges at statistik.tu-dortmund.de
Wed Dec 9 16:44:20 CET 2009
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:
> files\\Everything-newest.csv",header=T, sep=",", dec=".", na.strings=NA,
> tempdf<-data.frame(cbind(vcdf[,1:3], vcdf[,429]))
> groupmean<-tapply(newtemp[,4], newtemp[,2], mean)
> newmark<-factor(groupmean, exclude=(groupmean==0 | groupmean==1))
> 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
> 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.
> I just can't seem to figure this out by myself.
> Many thanks for your help.
> A Singh
> Aditi.Singh at bristol.ac.uk
> School of Biological Sciences
> University of Bristol
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help