问题
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