简体   繁体   English

如何将自定义函数 () 和 if_else 与 grep 一起使用以重新编码 R 中的值

[英]How to use custom function() and if_else with grep to recode values in R

I am trying to create a custom function that I can apply across various columns to recode values from characters to numeric data.我正在尝试创建一个自定义 function,我可以将其应用于各个列以将值从字符重新编码为数字数据。 The data has many blanks and each character entry is the same in each given column (ie. when there is a survey question that is "select all that apply" so you need to create binary 1/0 variables if the choice was selected).数据有很多空白,每个给定列中的每个字符条目都相同(即,当调查问题是“选择所有适用项”时,如果选择了该选项,则需要创建二进制 1/0 变量)。 So logical i am trying to create a function that does the following:如此合乎逻辑,我正在尝试创建一个执行以下操作的 function:

"In a specified range of columns in the data, if there is any character present, recode that entry as 1, otherwise mark as NA" “在指定范围的数据列中,如果存在任何字符,则将该条目重新编码为 1,否则标记为 NA”

This works as a standalone function as follows perfectly:这作为一个独立的 function 完美地工作如下:

data$var <- if_else(data$var == data$var[grep("[a-z]", data$var)], 1, NULL)

But I am having trouble creating a function that does this that I can apply to many different columns.但是我在创建一个 function 时遇到了问题,它可以应用于许多不同的列。

I have tried to solve this with lapply, mutate, and if_else in the following ways to no avail.我尝试通过以下方式使用 lapply、mutate 和 if_else 解决此问题,但无济于事。

I can return the indices correctly with the following fxn but need to update the actual dataframe:我可以使用以下 fxn 正确返回索引,但需要更新实际的 dataframe:

fxn <- function(x) {
  if_else(x == (x[grep("[a-z]", x)]), 1, NULL)
}

fxn(data$variable)

But when I try to use mutate to update the dataframe as follows it doesn't work:但是当我尝试使用 mutate 更新 dataframe 时,它不起作用:

data %>% 
  mutate(across(.cols = variable, fxn))

Any help would be appreciated as there are 100+ columns I need to do this on!任何帮助将不胜感激,因为我需要在 100 多列上执行此操作!

We create the function and apply to the selected columns with lapply .我们创建 function 并使用lapply应用于选定的列。 In the below example, columns 1 to 5 are selected and applied the function, and assigned back在下面的示例中,选择了第 1 到第 5 列并应用了 function,然后分配回来

fxn <- function(x) NA^(!grepl('[a-z]', x))
data[1:5] <- lapply(data[1:5], fxn)

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

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