简体   繁体   English

使用“across”更改 dplyr 中的多个列

[英]Using “across” to change multiple columns in dplyr

I have 100 columns that are all numeric.我有 100 列都是数字的。 The first column is an ID number, which needs to stay numeric, and I need to change the other columns to factors.第一列是一个 ID 号,需要保持数字,我需要将其他列更改为因子。 I have been trying to use the new(ish) across function from dplyr to do this, but I cannot successfully apply the function to all columns except the first one.我一直在尝试使用 function 中的新(ish) across dplyr执行此操作,但我无法成功地将 function 应用于除第一列之外的所有列。 From what I've read, I should be able to do the following:根据我的阅读,我应该能够做到以下几点:

df %>% mutate(across(everything(!c(ID)), as.factor))

But this gives the error: "Can't subset columns that don't exist. Locations 101, 102, etc. do not exist."但这会产生错误:“不能对不存在的列进行子集化。位置 101、102 等不存在。” What am I doing wrong?我究竟做错了什么?

Instead of the negate ( ! ) in everything , we just need - or as @27 ϕ 9 mentioned !而不是everything的否定( ! ),我们只需要-或@27 φ 9 提到的! also works without the everything没有everything也可以工作

library(dplyr)
df <- df %>%
     mutate(across(-ID, factor))

If there are more than one column, wrap it inside c如果多于一列,将其包裹在c

df <- df %>%
        mutate(across(-c(ID, ID2), factor))

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

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