本文介绍了如何从 dplyr 中的对象调用列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 dplyr 将多列中的所有零替换为 NA.但是,由于我有很多变量,我不想一个一个地调用它们,而是将它们存储在一个对象中,以便以后调用.

I am trying to replace all zeros in multiple columns with NA using dplyr.However, since I have many variables, I do not want to call them all by one, but rather store them in an object that I can call afterwards.

这是我所做的一个最小示例:

This is a minimal example of what I did:

library(dplyr)

Data <- data.frame(var1=c(1:10), var2=rep(c(0,4),5), var3 = rep(c(2,0,3,4,5),2), var4 = rep(c(7,0),5))

col <- Data[,c(2:4)]

Data <- Data %>%
  mutate(across(col , na_if, 0))

但是,如果我这样做,我会收到以下错误消息:

However, if I do this, I get the following error message:

Error: Problem with 'mutate()' input '..1'.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type 'data.frame<

  var2: double

  var3: double

  var4: double>'.

i It must be numeric or character.

i Input '..1' is '(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...'.

我尝试将 col 的格式更改为 tibble,但这没有帮助.

I have tried to change the format of col to a tibble, but that did not help.

谁能告诉我如何进行这项工作?

Could anyone tell me how to make this work?

推荐答案

这里,col应该是Data的名字.由于 col 有一个函数名,我们可以对对象进行不同的命名,用 all_of 包装并将 中的 0 替换为 NA跨越

Here, the col should be names of the Data. As there is a function name with col, we can name the object differently, wrap with all_of and replace the 0 to NA within across

library(dplyr)
col1 <- names(Data)[2:4]
Data <- Data %>%
   mutate(across(all_of(col1) , na_if, 0))

-输出

Data
#   var1 var2 var3 var4
#1     1   NA    2    7
#2     2    4   NA   NA
#3     3   NA    3    7
#4     4    4    4   NA
#5     5   NA    5    7
#6     6    4    2   NA
#7     7   NA   NA    7
#8     8    4    3   NA
#9     9   NA    4    7
#10   10    4    5   NA

注意:这里 OP 询问基于索引或列名的循环

NOTE: Here the OP asked about looping based on either the index or the column names

这篇关于如何从 dplyr 中的对象调用列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:39