Mark Na
2009-Jun-30 20:20 UTC
[R] How to wrap my (working) code in a loop or function? (loop/function newbie alert)
Dear R-helpers, I have split a dataframe into a list with five elements, with the following code:> datalist<-split(data,data$UNIT)I would now like to run some code (below) on each element of the list to extract rows from the list elements; then I would like to rbind the extracted rows into a new dataframe containing all of the extracted rows from all of the list elements. I don't need any help with the code itself, it works fine for one chunk of data (e.g., a single dataframe). The code is: t0<-match(times$START_DT, data$DATETIME) #MAKE A VECTOR OF START TIMES t1<-match(times$STOP_DT, data$DATETIME) #MAKE A VECTOR OF STOP TIMES indices<-mapply(FUN = ":", t0, t1) #MAKES A LIST, EACH ELEMENT CONTAINS INDICES OF TIMES CORRESPONDING TO ONE WETLAND idex<-times[rep(1:nrow(times), sapply(indices, length)), c("POND_ID","OBS","REP","PID"), drop = FALSE] #MAKES A DATAFRAME tm<-data[unlist(indices), ] #FLATTENS THE LIST OF INDICES INTO A DATAFRAME extracted<-cbind(idex, tm) #BIND IDEX AND TM But now that I've split my data into a list with five elements, what I don't know how to do is wrap my code in a loop or function so I can run it on each of the five list elements and then rbind the extracted rows together into a new dataframe. (What I have now is 5 replicates of the above code, and I would like to replace that with a loop or function.) I have spent all morning on this, without much progress, so would appreciate any help you might be able to provide. Thanks! Mark Na [[alternative HTML version deleted]]
jim holtman
2009-Jun-30 20:29 UTC
[R] How to wrap my (working) code in a loop or function? (loop/function newbie alert)
Something like this where you use lapply to pass in each element of the list to a function and then rbind the result: datalist <- split(data,data$UNIT) result <- lapply(datalist, function(.unit){ t0<-match(times$START_DT, .unit$DATETIME) #MAKE A VECTOR OF START TIMES t1<-match(times$STOP_DT, .unit$DATETIME) #MAKE A VECTOR OF STOP TIMES indices<-mapply(FUN = ":", t0, t1) #MAKES A LIST, EACH ELEMENT CONTAINS INDICES OF TIMES CORRESPONDING TO ONE WETLAND idex<-times[rep(1:nrow(times), sapply(indices, length)), c("POND_ID","OBS","REP","PID"), drop = FALSE] #MAKES A DATAFRAME tm<-.unit[unlist(indices), ] #FLATTENS THE LIST OF INDICES INTO A DATAFRAME extracted<-cbind(idex, tm) #BIND IDEX AND TM }) result <- do.call(rbind, result) On Tue, Jun 30, 2009 at 4:20 PM, Mark Na <mtb954@gmail.com> wrote:> Dear R-helpers, > > I have split a dataframe into a list with five elements, with the following > code: > > > datalist<-split(data,data$UNIT) > > I would now like to run some code (below) on each element of the list to > extract rows from the list elements; then I would like to rbind the > extracted rows into a new dataframe containing all of the extracted rows > from all of the list elements. > > I don't need any help with the code itself, it works fine for one chunk of > data (e.g., a single dataframe). The code is: > > t0<-match(times$START_DT, data$DATETIME) #MAKE A VECTOR OF START TIMES > t1<-match(times$STOP_DT, data$DATETIME) #MAKE A VECTOR OF STOP TIMES > indices<-mapply(FUN = ":", t0, t1) #MAKES A LIST, EACH ELEMENT CONTAINS > INDICES OF TIMES CORRESPONDING TO ONE WETLAND > idex<-times[rep(1:nrow(times), sapply(indices, length)), > c("POND_ID","OBS","REP","PID"), drop = FALSE] #MAKES A DATAFRAME > tm<-data[unlist(indices), ] #FLATTENS THE LIST OF INDICES INTO A DATAFRAME > extracted<-cbind(idex, tm) #BIND IDEX AND TM > > But now that I've split my data into a list with five elements, what I > don't > know how to do is wrap my code in a loop or function so I can run it on > each > of the five list elements and then rbind the extracted rows together into a > new dataframe. > > (What I have now is 5 replicates of the above code, and I would like to > replace that with a loop or function.) > > I have spent all morning on this, without much progress, so would > appreciate > any help you might be able to provide. > > Thanks! Mark Na > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]