Naveen Srikanth Naveen Srikanth - 1 month ago 7
R Question

Computing new attribute for a list of multiple dataframes and unlists

I request your help in R to resolve the issue.
I have dataframes as given 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 dataframe as given below

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


As read from another Stack Overflow answer, it is advised to put in a list of all the dataframes. I was trying use the below statement to do so. It worked

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


Now I am not able to compute the new attribute with below code 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 reform my dataframes without a list.

Answer

In R, it is better keep the data.frames in a list. If we really need to update the data.frames objects in the global environment, use list2env after transforming the 'df_list' with a 'value' column.

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
#  a b c d value
#1 1 2 3 4  2.28
#2 1 2 3 4  2.28
#3 1 2 3 4  2.28
 df2
#  a b c d 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 values of the string (ls(pattern='df\\d+'))) in a list using mget, then loop through the list of data.frames (lapply(df_list, ...), transform to create a new column 'value' in each of the data.frame and finally update the objects in the global environment with 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")