Compute a new attribute for a list of multiple data frames and exceptions

I ask your help in R to solve the problem. I have dataframes as below

df1 a,b,c,d 1,2,3,4 1,2,3,4 1,2,3,4 df2 a,b,c,d 1,2,3,4 1,2,3,4 1,2,3,4 1,2,3,4 

I need to perform an operation on each data frame as follows

  df1$value <- 0.12*df1$a+0.24*df1$b+0.56*df1$c 

As read from another stack overflow answer, it is recommended that you insert a list of all data frames. I tried to use the expression below for this. He worked

 df_list <- list(ls(pattern='df*')) 

Now I can not calculate the new attribute using the code below using lapply

 res <- lapply(dflist, function(x) { 0.12*grep(x[[a]])+0.24*grep(x[[b]])+0.56*grep(x[[c]])) }) 

After performing the above operation, I want to change my data without a list.

+6
source share
2 answers

In R it is better to save data.frames in list . If we really need to update the data.frames objects in the global environment, use list2env after transform , using "df_list" with the column "value".

 df_list <- mget(ls(pattern='df\\d+')) res <- lapply(df_list, transform, value = 0.12*a + 0.24*b + 0.56*c) list2env(res, envir = .GlobalEnv) df1 # abcd value #1 1 2 3 4 2.28 #2 1 2 3 4 2.28 #3 1 2 3 4 2.28 df2 # abcd value #1 1 2 3 4 2.28 #2 1 2 3 4 2.28 #3 1 2 3 4 2.28 #4 1 2 3 4 2.28 

We get the row values ​​( ls(pattern='df\\d+')) ) in list using mget , then scroll through the list from data.frame ( lapply(df_list, ... ), transform to create a new column 'value' in each of data.frame and finally update the objects in the global environment using list2env .

data

 df1 <- structure(list(a = c(1L, 1L, 1L), b = c(2L, 2L, 2L), c = c(3L, 3L, 3L), d = c(4L, 4L, 4L)), .Names = c("a", "b", "c", "d"), class = "data.frame", row.names = c(NA, -3L)) df2 <- structure(list(a = c(1L, 1L, 1L, 1L), b = c(2L, 2L, 2L, 2L), c = c(3L, 3L, 3L, 3L), d = c(4L, 4L, 4L, 4L)), .Names = c("a", "b", "c", "d"), row.names = c(NA, -4L), class = "data.frame") 
+2
source

Here is a matrix multiplication solution:

 df1 <- read.table(header=TRUE, sep=",", text= "a,b,c,d 1,2,3,4 1,2,3,4 1,2,3,4") df2 <- read.table(header=TRUE, sep=",", text= "a,b,c,d 1,2,3,4 1,2,3,4 1,2,3,4 1,2,3,4") df1$value <- as.matrix(df1) %*% c(0.12, 0.24, 0.56, 0) df1 df2$value <- as.matrix(df2) %*% c(0.12, 0.24, 0.56, 0) df2 

or

 df1$value <- as.matrix(df1[1:3]) %*% c( 0.12, 0.24, 0.56) df2$value <- as.matrix(df2[1:3]) %*% c( 0.12, 0.24, 0.56) 

To work with the dataframes list, you can:

 L <- list(df1, df2) lapply(L, function(x) transform(x, value=as.matrix(x[1:3]) %*% c( 0.12, 0.24, 0.56))) 
+2
source

All Articles