5

I would like to apply the same operation to multiple data frames in 'R' but cannot get how to deal with this matter.

This is an example of pipe operation in dplyr:

library(dplyr)

    iris %>% mutate(Sepal=rowSums(select(.,starts_with("Sepal"))),
                    Length=rowSums(select(.,ends_with("Length"))),
                    Width=rowSums(select(.,ends_with("Width"))))
iris2 <- iris
iris3 <- iris

Could you suggest how to apply the same pipe function to iris, iris2 and isis3? I need to use dplyr piping operation.

I suppose map function may help but as I have not fully understand its concept, I got errors to apply it.

Sample script:

library(purrr)

iris.set <- c(iris,iris2,iris3)
map(iris.set, ~ . %>% mutate(Sepal=rowSums(select(.,starts_with("Sepal"))),
                Length=rowSums(select(.,ends_with("Length"))),
                Width=rowSums(select(.,ends_with("Width")))))
www
  • 38,575
  • 12
  • 48
  • 84
HSJ
  • 687
  • 6
  • 16
  • 2
    Make the following changes: `iris.set <- list(iris,iris2,iris3)` `map(iris.set, ~ .x %>% mutate(Sepal=rowSums(select(.,starts_with("Sepal"))), Length=rowSums(select(.,ends_with("Length"))), Width=rowSums(select(.,ends_with("Width")))))` – CPak Jun 14 '18 at 13:10
  • @CPak Great! So I need to unlist `iris.set` if I need to keep those data frames individually? – HSJ Jun 14 '18 at 13:13
  • What do you mean keep them individually? – CPak Jun 14 '18 at 14:38
  • I mean that the resulted list contains 3 data frames, `iris`, `iris2` and `iris3`. I need them back to 3 data frames again stored in `data.frame`, not `list`. I created new post for this specific issue. https://stackoverflow.com/questions/50860018/how-to-split-data-frames-in-a-list-into-multiple-data-frames-in-r – HSJ Jun 14 '18 at 14:40

2 Answers2

9

If you turn your operation into a function:

library(dplyr)
my_fun <- function(x) { 
      x %>%       
          mutate(Sepal=rowSums(select(.,starts_with("Sepal"))),
          Length=rowSums(select(.,ends_with("Length"))),
          Width=rowSums(select(.,ends_with("Width"))))
}

You can pipe a list of data frames to it easily:

result <- list( iris, iris2, iris3 ) %>%
    lapply( my_fun )
rosscova
  • 5,430
  • 1
  • 22
  • 35
  • Thank you for your suggestion but I prefer to keep using `dplyr` for easy operation as suggested by @www. – HSJ Jun 14 '18 at 13:46
  • 1
    Hi @rosscova - any way I can replace the existing 'iris' dataframes instead of combining them into 'result'? –  Sep 26 '19 at 15:46
1

is it good for you ?

    library(dplyr)

    #matrices replication 
    iris1=iris
    iris2=iris
    iris3=iris

    #list of combinations: apply is tricky for array input
    irises=matrix(c("iris1","iris2","iris3"), ncol=1)

    #function design
    Funct<-function(df_name){

      df=get(df_name)
      df %>% mutate(Sepal=rowSums(select(.,starts_with("Sepal"))),
                    Length=rowSums(select(.,ends_with("Length"))),
                    Width=rowSums(select(.,ends_with("Width"))))
    }

    apply(irises,MARGIN=2, Funct)
Amara BOUDIB
  • 343
  • 2
  • 6