lapply and mutate_all/for loops

泪湿孤枕 提交于 2020-07-16 08:02:18

问题


I have several data frames within a list which I have to modify by normalizing all the data, in all columns (basically, divide each row/column by the sum of the number of that column).

After loading all my raw data frames with lapply I want to iterate over all columns to perform such operation (i.e. mutate(df, df$my_column=df$my_column/sum(df$my_column))).

My code is:

samplelist <- list(df1 = "path to df1",
                 df2 = "path to df2",
                 df3 = "path to df3")


samples <- lapply(names(samplelist),function(processing){
  aux <- read.csv(samplelist[[processing]], header = T, sep = "") # works
  for (i in colnames(aux)){
    mutate(aux, aux[[i]]=aux[[i]]/sum(aux[[i]]))
  }
})

But doesn't work (unexpected "=" and later on unexpected "{"), so I have tried to use mutate_all from dplyr, but I don't really know how to pipe it

samplelist <- list(df1 = "path to df1",
                     df2 = "path to df2",
                     df3 = "path to df3")


    samples <- lapply(names(samplelist),function(processing){
      aux <- read.csv(samplelist[[processing]], header = T, sep = "") %>% mutate_all(what should I write there?)
    })

I could probably just add a new line and use mutate_all, but still I can't figure out what arguments to give. If you know also other ways to do it is fine.

Many thanks for your help.


回答1:


You can just use your described function and adapt for the function argument. In dplyr, the . stands, in this case, for the variable. The ~ defines a formula.

samples <- lapply(names(samplelist), function(processing){
  aux <- read.csv(samplelist[[processing]], header = T, sep = "") %>% 
    mutate_all(~./sum(.))
})


来源:https://stackoverflow.com/questions/58720178/lapply-and-mutate-all-for-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!