简体   繁体   中英

R: add a calculated column to a list of dataframes

I have two lists of data frames. I want to do some calculations on the first list, and then apply the results to the second list

# first list
df1 <- data.frame(id=1:5, score=c(rep(1, 3), rep(0, 2)))  
df2 <- data.frame(id=1:5, score=c(rep(1, 4), rep(0, 1)))
df1
   id score
1  1     1
2  2     1
3  3     1
4  4     0
5  5     0

df2
id score
1  1     1
2  2     1
3  3     1
4  4     1
5  5     0

list1 <- list(df1, df2)

# second list
df3 <- data.frame(id =1 :3)  
df4 <- data.frame(id =1 :4)    
list2 <- list(df3, df4)

I calculate scores for each of the dataframes in list1:

scores <- sapply(list1, function(df) sum(select(df, score))/nrow(df) )  
scores
[1] 0.6 0.8

Now i want to update the dataframes in list2 with the scores, to get the following: The first score is applied to the first dataframe and the second to the second and so on.

df3
  id   score
1  1   0.6
2  2   0.6
3  3   0.6 

df4
  id   score
1  1   0.8
2  2   0.8
3  3   0.8
4  4   0.8 

I've tried using lapply on list2, I'm thinking of something along the lines of

list2 <- lapply(list2, function(df){ df$score <- 1; df})  

Obviously with the appropriate score instead of 1. This updates the dfs within the list, but a) I can't get it to update the dataframes df3 and df4. b) I can't see how to pass the calculated scores to the lapply function

Help appreciated. TIA.

我们可以使用Map

Map(cbind, list2, score = scores)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM