[R] subsetting question

Ivan Calandra ivan.calandra at univ-reims.fr
Wed May 20 16:31:35 CEST 2015


Hi,

What about using functions like aggregate()?
Something like:
aggregate(Weight~datums, data=dataset1, FUN=mean)

If you need to do more things, you can create your own function for 'FUN'

HTH,
Ivan

--
Ivan Calandra, ATER
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra

Le 20/05/15 12:03, Dieter Anseeuw a écrit :
> Dear all,
> I would like to do multiple actions on a subset of my data. Therefore, I want to create a for loop on the variable "Date" (actually a double for loop on yet another variable, but let's omit that for a moment).
> I want to run down every level of "Date" and perform multiple actions on the data from a certain date. Here is my code:
>
> for (i in 1:length(datums)){
> meanweight<-mean(dataset1[dataset1$Date==datums[i],]$Weight)
> ...
>
> However, this subsetting obviously doesn't work. How can I adjust my code so that R runs down all levels of Data in a for loop?
> (I need the for loop, not tapply(), sapply(), ...)
>
> Thanks in advance,
> Dieter Anseeuw
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



More information about the R-help mailing list