首页 > 解决方案 > 将多个函数应用于 R 中的数据框

问题描述

我正在尝试将多个函数应用于 R 中的数据框列,如下所示:

lapply(targets$phenotype, function(x) {tolower(x); gsub(" ", "_",x) })

但是,只有第二个最终运行。如果我切换 tolower 和 gsub 的顺序(只有第二个运行),也会发生同样的事情

标签: rdataframe

解决方案


tolower更新为x

lapply(targets$phenotype, function(x) {x <- tolower(x); gsub(" ", "_",x) })

gsub在这里,如果它是单列并且tolower被矢量化,我们甚至不需要循环

targets$phenotype <- gsub(" ", "_", tolower(targets$phenotype))

推荐阅读