Mark Na
2009-Jul-23 18:05 UTC
[R] How to perform a calculation in each element of my list?
Hi R-helpers, I have a list containing 10 elements, each of which is a dataframe. I wish to add a new column to each list element (dataframe) containing the product of the last two columns of each dataframe. I'd appreciate any pointers, thanks! Mark Na [[alternative HTML version deleted]]
Jorge Ivan Velez
2009-Jul-23 18:14 UTC
[R] How to perform a calculation in each element of my list?
Dear Mark, Try this: lapply(yourlistofdataframes, function(d){ k <- ncol(d) d$product <- d[,k-1] * d[,k] d } ) HTH, Jorge On Thu, Jul 23, 2009 at 2:05 PM, Mark Na <mtb954@gmail.com> wrote:> Hi R-helpers, > > I have a list containing 10 elements, each of which is a dataframe. I wish > to add a new column to each list element (dataframe) containing the product > of the last two columns of each dataframe. > > I'd appreciate any pointers, 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 > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Daniel Malter
2009-Jul-23 18:21 UTC
[R] How to perform a calculation in each element of my list?
Hi Mark, study the following example. The two simulated dataframes are put in a list called listdata. The loop iterates throught the elements in the list and multiplies the last column "listdata[[i]][,length(listdata[[i]])]" by the column before the last "listdata[[i]][,length(listdata[[i]])-1]". This new columns is assigned to be the new column after the last of the respective dataframe in the list "listdata[[i]][,length(listdata[[i]])+1]" x1=rnorm(100,0,1) x2=rnorm(100,0,1) x3=rnorm(100,0,1) x4=rnorm(100,0,1) data1=data.frame(x1,x2) data2=data.frame(x3,x4) listdata=list(data1,data2) for(i in 1:length(listdata)){ listdata[[i]][,length(listdata[[i]])+1]=listdata[[i]][,length(listdata[[i]]) ]*listdata[[i]][,length(listdata[[i]])-1] } HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Mark Na Gesendet: Thursday, July 23, 2009 2:06 PM An: r-help at r-project.org Betreff: [R] How to perform a calculation in each element of my list? Hi R-helpers, I have a list containing 10 elements, each of which is a dataframe. I wish to add a new column to each list element (dataframe) containing the product of the last two columns of each dataframe. I'd appreciate any pointers, thanks! Mark Na [[alternative HTML version deleted]] ______________________________________________ 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.
Erik Iverson
2009-Jul-23 18:42 UTC
[R] How to perform a calculation in each element of my list?
Mark, My example is essentially identical to Jorge's. This is a good opportunity to compare two solutions to a problem, one using "for" loops, and one using the apply family of functions. Compare this with Daniel's solution. ## BEGIN EXAMPLE ## sample list of data.frames, different number of columns in each one lst <- sapply(c(20, 25), function(x) as.data.frame(matrix(1:100, nrow = x))) ## define function to do what you want for one data.frame, needs at least 2 ## columns, no checks for that or for them being numeric... mult2cols <- function(x) { nc <- ncol(x) x$product <- x[,nc] * x[,nc-1] x } ## apply the function to your list of data.frames lst <- lapply(lst, tmp) ## END EXAMPLE --erik -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Na Sent: Thursday, July 23, 2009 1:06 PM To: r-help at r-project.org Subject: [R] How to perform a calculation in each element of my list? Hi R-helpers, I have a list containing 10 elements, each of which is a dataframe. I wish to add a new column to each list element (dataframe) containing the product of the last two columns of each dataframe. I'd appreciate any pointers, thanks! Mark Na [[alternative HTML version deleted]] ______________________________________________ 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.