0

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.

Michael Spector
  • 113
  • 1
  • 7
  • 2
    It's working as you expect it to. I just tried your code on sample data: `df1 <- data.frame(v1=c(1,2),v2=c(3,4)); df2 <- data.frame(v1=c(2,2),v2=c(3,2)); df3 <- data.frame(v1=c(1,1),v2=c(4,4)); m=list(df1,df2,df3); mean_values <- list(c(5,5),c(6,6),c(7,7)); for(i in 1:length(m)){m[[i]]$means = mean_values[[i]]}; m`. – Prem Jul 12 '17 at 18:02
  • It works, but the changes doesn't appear to be in the original data.frames. how can apply this changes to the original data.frames ? – Michael Spector Jul 12 '17 at 18:11
  • I personally would keep them in the list for convenience, but if you want them back out, try the solution from [this answer](https://stackoverflow.com/questions/17697239/unlist-a-list-of-dataframes)- try `list2env(m, .GlobalEnv)`. – Luke C Jul 12 '17 at 18:33
  • I tried. but it shows me an error: `Error in list2env(m, .GlobalEnv) : names(x) must be a character vector of the same length as x` – Michael Spector Jul 12 '17 at 18:41
  • 1
    @MichaelSpector - Oh right- you created `m` as an unnamed list. If you do `m = list(X2016_kvish_1_10t = X2016_kvish_1_10t, X2015_kvish_1_10t = X2015_kvish_1_10t, ...)` it should work. – Luke C Jul 12 '17 at 18:52
  • 1
    Thanks a lot !!! It works :) I would have buy you a present but I guess you live in the other side of the world :) see you tomorrow with my new questions good night – Michael Spector Jul 12 '17 at 19:00

1 Answers1

0

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.

Luís Telles
  • 694
  • 3
  • 13