-1

I created a list of data frames. I'm trying to change the original data frames with mutate and select commands. I created a list from all my data frames

m <- as.list(paste0(rep("traffic_", 12), 2005:2016))

I Tried to run the next command: but it still doesn't work for me.

foreach(x=iter(m)) %do% { assign(x, mutate(x , date = make_datetime(shana, hodesh, taarich, shaa)) %>%
                               select( -shana, -hodesh, -taarich, -shaa)) }

this is one of the original data frames

> traffic_2016
  shana kvish keta maslul hodesh taarich yom shaa nefah status
1      2016     1   10      1      9      11   1    0  1710     NA
2      2016     1   10      1      9      11   1    1   934     NA
3      2016     1   10      1      9      11   1    2   800     NA
4      2016     1   10      1      9      11   1    3   637     NA
5      2016     1   10      1      9      11   1    4   588     NA
6      2016     1   10      1      9      11   1    5   951     NA
7      2016     1   10      1      9      11   1    6  2312     NA
8      2016     1   10      1      9      11   1    7  3769     NA
9      2016     1   10      1      9      11   1    8  3348     NA
10     2016     1   10      1      9      11   1    9  2788     NA
11     2016     1   10      1      9      11   1   10  2879     NA
Michael Spector
  • 113
  • 1
  • 7
  • A mutation in the dplyr context isn't a mutation in the sense of memory modification in place. In this context, it is a transformation. You'll have to assign the result of your transformation to an object, so try `m[[i]] = mutate(i , date = ...`. – Akhil Nair Jul 11 '17 at 14:22
  • Try to design a function and use `lapply` to apply it to your list. It could be more efficient. – www Jul 11 '17 at 14:24
  • You also probably want to make your `m` object this way: `m = list(noquote(paste0(rep("traffic_", 12), 2005:2016)))` instead of writing all those traffic words. – rnorouzian Jul 11 '17 at 14:26
  • It would be great if you can provide reproducible example dataset, and show the code to load `dplyr` and `lubridate` packages. – www Jul 11 '17 at 14:28
  • https://stackoverflow.com/questions/44208873/r-referencing-different-dataframes-in-a-loop/44209590#44209590 hope the link will help – BENY Jul 11 '17 at 14:30
  • I tried what you said but it didn't work – Michael Spector Jul 11 '17 at 14:30
  • @MichaelSpector if my answer helped solve your issue please consider accepting it as an answer by clicking on the check mark to the left of my answer. This lets the community know the answer worked for you. – CPak Sep 09 '17 at 02:07

1 Answers1

2

You want to edit each of the original data frames:

mtcars1 <- mtcars
mtcars2 <- mtcars

m <- list("mtcars1","mtcars2")

You can use assign to edit data by name. I use get to edit the actual data:

library(iterators)
library(foreach)
library(dplyr)
foreach(x=iter(m)) %do% { assign(x, mutate(get(x), mpg=mpg+1) %>% select(mpg,cyl)) }

Using your mutate:

foreach(x=iter(m)) %do% { assign(x, mutate(get(x) , date = make_datetime(shana, hodesh, taarich, shaa)) %>% select( -shana, -hodesh, -taarich, -shaa)) }
CPak
  • 13,260
  • 3
  • 30
  • 48