lapply和mutate_all/for循环 [英] lapply and mutate_all/for loops

查看:48
本文介绍了lapply和mutate_all/for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表中有几个数据框,我必须通过规范化所有列中的所有数据来进行修改(基本上,将每一行/列除以该列数的总和).

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).

在用lapply加载所有原始数据帧之后,我想遍历所有列以执行此类操作(即mutate(df,df $ my_column = df $ my_column/sum(df $ my_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))).

我的代码是:

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]]))
  }
})

但是不起作用(意外的"="以及后来出现的意外的"{"),因此我尝试使用dplyr的mutate_all,但我真的不知道该如何使用管道

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?)
    })

我可能只需要添加新行并使用mutate_all,但仍然无法弄清楚要给出什么参数.如果您还知道其他方法也可以.

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.

非常感谢您的帮助.

推荐答案

您可以只使用所描述的函数并适应该函数的参数.在dplyr中,在这种情况下,.代表变量.定义一个公式.

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(.))
})

这篇关于lapply和mutate_all/for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆