简体   繁体   English

跨多个列运行函数

[英]Run a function across multiple columns

I'm trying to clean a sixty column data extract that has been given to me. 我正在尝试清除已提供给我的六十列数据摘录。 Part of the data is about thirty columns which have been supplied as "Yes" or "No" values that I would like to convert to logical type. 数据的一部分大约是三十列,这些列已作为“是”或“否”值提供,我希望将其转换为逻辑类型。 It therefore isn't every column in the data frame, but it is a lot of them. 因此,它并不是数据框中的每一列,但其中有很多。 I'm currently doing the equivalent of this: 我目前正在这样做:

mtcars %>%
  mutate(mpg = as.character(mpg)) %>%
  mutate(cyl = as.character(cyl)) %>%
  mutate(disp = as.character(disp)) %>%
  mutate(hp = as.character(hp))

That is, manually mutating each column in the list. 也就是说,手动更改列表中的每一列。 But that feels like it will be prone to error from missing a copy-paste or similar. 但这感觉很容易由于缺少复制粘贴或类似内容而出错。 Is there a function that could do this in one step by being passed a list of field names? 是否有一个函数可以通过传递字段名称列表来一步完成此操作? I tend to default to tidyverse functions, though base R also works if needed. 我倾向于默认使用tidyverse函数,尽管如果需要,基数R也可以使用。

This should be a duplicate but cannot find a relevant post right now. 这应该是重复的,但现在找不到相关的帖子。

We can use mutate_at and apply function on selected columns 我们可以使用mutate_at并将功能应用于选定的列

library(dplyr)
mtcars %>% mutate_at(vars(mpg, cyl, disp, hp), as.character)

Or if we have column names stored in vector called cols we could do 或者,如果我们将列名称存储在称为cols向量中,则可以执行

cols <- c("mpg", "cyl", "disp", "hp")
mtcars %>% mutate_at(cols, as.character)

Perhaps you can use lapply() ? 也许您可以使用lapply()

lapply(mtcars, as.character)

If you would like your data as a data frame: 如果您希望将数据作为数据框:

df = as.data.frame( lapply(mtcars, as.character), stringsAsFactors = F )

> df$mpg
 [1] "21"   "21"   "22.8" "21.4" "18.7" "18.1" "14.3" "24.4" "22.8"
[10] "19.2" "17.8" "16.4" "17.3" "15.2" "10.4" "10.4" "14.7" "32.4"
[19] "30.4" "33.9" "21.5" "15.5" "15.2" "13.3" "19.2" "27.3" "26"  
[28] "30.4" "15.8" "19.7" "15"   "21.4"

> df$cyl
 [1] "6" "6" "4" "6" "8" "6" "8" "4" "4" "6" "6" "8" "8" "8" "8" "8"
[17] "8" "4" "4" "4" "4" "8" "8" "8" "8" "4" "4" "4" "8" "6" "8" "4"

> df$disp
 [1] "160"   "160"   "108"   "258"   "360"   "225"   "360"   "146.7"
 [9] "140.8" "167.6" "167.6" "275.8" "275.8" "275.8" "472"   "460"  
[17] "440"   "78.7"  "75.7"  "71.1"  "120.1" "318"   "304"   "350"  
[25] "400"   "79"    "120.3" "95.1"  "351"   "145"   "301"   "121"  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM