简体   繁体   中英

mutate column for multiple dataframes from a list in r

I have a list of 12 data.frames:

m  =list(X2016_kvish_1_10t, X2015_kvish_1_10t, X2014_kvish_1_10t,
     X2013_kvish_1_10t, X2012_kvish_1_10t, X2011_kvish_1_10t,
     X2010_kvish_1_10t, X2009_kvish_1_10t, X2008_kvish_1_10t)
     X2007_kvish_1_10t, X2006_kvish_1_10t, X2005_kvish_1_10t)

and I have a list of 12 vectors called mean_values . Output from str(mean_values) :

 List of 12
$ : Named num [1:168] 2848 2848 2848 2848 2848 ...
 ..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 2870 2870 2870 2870 2870 ...
..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 2911 2911 2911 2911 2911 ...
 ..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3422 3422 3422 3422 3422 ...
..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : NULL
$ : Named num [1:168] 2747 2747 2747 2747 2747 ...
..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3234 3234 3234 3234 3234 ...
..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3334 3334 3334 3334 3334 ...
..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3440 3440 3440 3440 3440 3440 3440 3440 3440 3440 ...
..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3327 3327 3327 3327 3327 ...
 ..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3440 3440 3440 3440 3440 ...
 ..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...
$ : Named num [1:168] 3497 3497 3497 3497 3497 ...
 ..- attr(*, "names")= chr [1:168] "a" "a" "a" "a" ...

I need to add each vector to new column in each data.frame from the list. when I do separately it works well:

X2016_kvish_1_10t$mean_values = mean_values[[1]]
X2016_kvish_1_10t$mean_values = mean_values[[2]]
X2016_kvish_1_10t$mean_values = mean_values[[3]]
# ... until 12

but I need to find a way to add these vectors in one shot. I tried this function:

for(i in 1:length(m)){m[[i]]$means = mean_values[[i]]}

this function works great but only when I print the whole list of the dataframes. I need to find a way to apply the changes to the originals dateframes, each one s.

Despite not knowing why you would want to do that... I think I got what you want.

If the items of your list are named, you could try:

for(i in 1:length(m)){
    assign(names(m)[i], m[[i]])
}

If your list is not conveniently named, you may name it like this: names(m) <- ls()[grep(pattern = 'kvish', x = ls(), ignore.case = TRUE)]

This will work if the dataframes of yours are the only objects in your memory that have kvish in their names.

But you have to be careful with the order of the names. Check the output of ls()[grep(pattern = 'kvish', x = ls(), ignore.case = TRUE)] and see if it's on the same order as your dataframes of the list.

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