简体   繁体   English

如何使用 mutate_at 和嵌套的 ifelse 语句自动重新编码许多变量?

[英]How to automate recoding of many variables using mutate_at and nested ifelse statement?

There is a large data set consisting of repeated measures of the same variable on each subject.有一个大型数据集,由对每个主题的相同变量的重复测量组成。 An example data is as below示例数据如下

df<-data.frame(
"id"=c(1:5),
"ax1"=c(1,6,8,15,17),
"bx1"=c(2,16,8,15,17))

where "x1" is measured repeatedly so we can have "ax1", "bx1", "cx1" and so on.其中“x1”是重复测量的,所以我们可以有“ax1”、“bx1”、“cx1”等等。 I am trying to recode these variables.我正在尝试重新编码这些变量。 The plan is to recode 1 and any number on the range from 3 to 12 (inclusively) as 0 and recode 2 or any value greater than or equal to 13 as 1. Because it involves many variables I am making use of "mutate_at" to automate the recoding.计划是将 1 和 3 到 12(包括)范围内的任何数字重新编码为 0,并将 2 或任何大于或等于 13 的值重新编码为 1。因为它涉及许多变量,我正在使用“mutate_at”来自动重新编码。 Also, the numbers to take on the same code are not consecutive (eg 1 and 3-12 to be recoded as 0) so I used a nested "ifelse" statement.此外,采用相同代码的数字不是连续的(例如,1 和 3-12 被重新编码为 0),因此我使用了嵌套的“ifelse”语句。 I tried the following我尝试了以下

df1<-df %>% 
mutate_at(vars(ends_with("x1")),factor, 
        ifelse(x1>=3 & x1 <=12,0,ifelse(x1==1, 0,
               ifelse(x1==2, 1,0))))

However, this fails to work because R cannot recognize "x1".但是,这不起作用,因为 R 无法识别“x1”。 Any help on this is greatly appreciated in advance.提前非常感谢对此的任何帮助。 The expected output would look like预期的输出看起来像

> df1
   id ax1 bx1
1  1   0   1
2  2   0   1
3  3   0   0
4  4   1   1
5  5   1   1   

Using ifelse , we can proceed as follows:使用ifelse ,我们可以进行如下操作:

df %>% 
   mutate_at(vars(ends_with("x1")),~ifelse(. ==1 | . %in% 3:12,0,
                                           ifelse(. ==2 | .>=13,1,.)))
  id ax1 bx1
1  1   0   1
2  2   0   1
3  3   0   0
4  4   1   1
5  5   1   1

We can use case_when我们可以使用case_when

library(dplyr)

df %>% 
  mutate_at(vars(ends_with("x1")), ~case_when((. >= 3 & . <= 12) | . == 1 ~ 0,
                                               . >= 13 | . == 2 ~ 1))

#  id ax1 bx1
#1  1   0   1
#2  2   0   1
#3  3   0   0
#4  4   1   1
#5  5   1   1

Here is another solution similar to what you where attempting.这是另一个类似于您尝试的解决方案。 I just added the "or" operator ( | ) to make a simpler ifelse and removed the factor part from your code.我刚刚添加了“或”运算符( | )来制作更简单的ifelse并从代码中删除了factor部分。

library(dplyr)
df1<-df %>% 
  mutate_at(vars(ends_with("x1")), function(x)
            ifelse(x >= 3 & x <= 12 | x == 1,0,
                   ifelse(x >= 13 | x == 2, 1,0)))

#  id ax1 bx1
#1  1   0   1
#2  2   0   1
#3  3   0   0
#4  4   1   1
#5  5   1   1

If there are no other possible conditions apart from the ones you mention (for example, having zeros), I think you could simplify it more by just reducing it to the following:如果除了您提到的条件之外没有其他可能的条件(例如,有零),我认为您可以通过将其简化为以下内容来进一步简化它:

df1<-df %>% 
  mutate_at(vars(ends_with("x1")), function(x)
            ifelse(x >= 3 & x <= 12 | x == 1, 0, 1))

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

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