[R] subsetting question

MacQueen, Don macqueen1 at llnl.gov
Wed May 20 18:35:18 CEST 2015


Assuming datums is a vector of the unique dates in Date... perhaps
  datums <- sort(unique(dataset1$Date))

I usually set it up like this

for (i in 1:length(datums) ) {

  crnt.date <- datums[i]
  tmpdat <- subset(dataset1, Date==crnt.date)
  cat(i, format(crnt.date), 'dim(tmpdat)',dim(tmpdat),'\n\n')

 ## use tmpdat for the multiple actions

}

The extra step of creating a subset helps one check that everything is
working as expected. It has no noticeable effect on performance with
datasets of the size I normally work with.

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 5/20/15, 3:03 AM, "Dieter Anseeuw" <dieter.anseeuw at inagro.be> wrote:

>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