从r中的序数数据中创建虚拟变量

发布于 2025-01-22 14:36:43 字数 302 浏览 0 评论 0原文

我有一个序数变量,以下类别

非常有利(1) 有点有利(2) 有点不利(3) 非常不利(4) 不知道(8) 拒绝回答(9)

我想要我的输出二进制可显示的变量:

有利(1) 不利(0)

我想通过将“非常有利的”和“有利”组合在一起,以编码为“ 1”的新“有利”结果 还将“非常不利”和“有点有利”组合在一起,对新结果“不利”编码为“ 0”。

,我基本上想将“ 1” =“ 1”和“ 2” =“ 1” then,然后3“ =“ 0”和“ 4” =“ 0”

I have an ordinal variable with the following categories

very favorable (1)
somewhat favorable (2)
somewhat unfavorable (3)
very unfavorable (4)
don't know (8)
refuse to answer (9)

I want my output binary variable to display:

favorable (1)
unfavorable (0)

I want to do that by grouping together "very favorable" and "somewhat favorable" to the new "favorable" outcome coded in "1"
and also group together "very unfavorable" and "somewhat favorable" to new outcome "unfavorable coded as "0".

So basically I want to turn "1" = "1" and "2" = "1" then "3" = "0" and "4" = "0"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2025-01-29 14:36:43

这是dplyr带有case_when()的解决方案,对于创建假人确实很有用。

我的启动数据如下:

  # A tibble: 6 x 2
      participant category              
            <int> <chr>                 
    1           1 somewhat favorable (2)
    2           2 very unfavorable (4)  
    3           3 very favorable (1)    
    4           4 don't know (8)        
    5           5 very favorable (1)    
    6           6 somewhat favorable (2)

因此,基本上,当它检测到1或2时,它会将行值转换为“有利(1)”,而3或4则将“不利(0)”转换

data %>%  
  mutate(category = case_when(
    str_detect(category, "(1)|(2)") ~ "favorable (1)", 
    str_detect(category, "(3)|(4)") ~ "unfavorable (0)"))

为(8)和(9) s未指定,代码将其返回为NAS。
最终数据集如下:

# A tibble: 10 x 2
   participant category       
         <int> <chr>          
 1           1 favorable (1)  
 2           2 unfavorable (0)
 3           3 favorable (1)  
 4           4 NA             
 5           5 favorable (1)  
 6           6 favorable (1)  
 7           7 unfavorable (0)
 8           8 unfavorable (0)
 9           9 favorable (1)  
10          10 unfavorable (0)

Here's a dplyr solution with case_when() which is really useful for creating dummies.

My starting data is as follows:

  # A tibble: 6 x 2
      participant category              
            <int> <chr>                 
    1           1 somewhat favorable (2)
    2           2 very unfavorable (4)  
    3           3 very favorable (1)    
    4           4 don't know (8)        
    5           5 very favorable (1)    
    6           6 somewhat favorable (2)

So, basically, when it detects 1 or 2, it will convert the row value into "favorable (1)" and 3 or 4 into "unfavorable (0)"

data %>%  
  mutate(category = case_when(
    str_detect(category, "(1)|(2)") ~ "favorable (1)", 
    str_detect(category, "(3)|(4)") ~ "unfavorable (0)"))

Since (8) and (9) s not specified, the code returns them as NAs.
Final dataset is as follows:

# A tibble: 10 x 2
   participant category       
         <int> <chr>          
 1           1 favorable (1)  
 2           2 unfavorable (0)
 3           3 favorable (1)  
 4           4 NA             
 5           5 favorable (1)  
 6           6 favorable (1)  
 7           7 unfavorable (0)
 8           8 unfavorable (0)
 9           9 favorable (1)  
10          10 unfavorable (0)
手心的海 2025-01-29 14:36:43

我能想到的最简单的方法是多种方法是利用百分比的百分比。

例如,在基本r中:

data$column_to_recode = as.character(data$column_to_recode) #failing to do this may result in R coercing existing factors to numeric integers representing ranks
data$column_to_recode[which(data$column_to_recode %in% c(1,2))] = 1
data$column_to_recode[which(data$column_to_recode %in% c(3,4))] = 0
data$column_to_recode[which(!(data$column_to_recode %in% c(0:4)))] = NA #or whatever else you want to do with the values that aren't 1 through 4`

那么,如果您真的想要奖金点,则可以将其重新归因于一个因素变量,但我发现这通常过于过多。

data$column_to_recode = factor(data$column_to_recode,levels=c(0,1),ordered = TRUE)

我无法从您的原始问题中分辨出数字代码是否正常,或者您是否想使用字符字符串,但是适用相同的逻辑,例如:

data$column_to_recode[which(data$column_to_recode %in% c("(1) somewhat favorable","(2) somewhat unfavorable"))] = "Favorable"

应该为您提供所需的东西。

Lots of ways to do this, easiest way I can think of is making use of some %in%.

e.g, in base R:

data$column_to_recode = as.character(data$column_to_recode) #failing to do this may result in R coercing existing factors to numeric integers representing ranks
data$column_to_recode[which(data$column_to_recode %in% c(1,2))] = 1
data$column_to_recode[which(data$column_to_recode %in% c(3,4))] = 0
data$column_to_recode[which(!(data$column_to_recode %in% c(0:4)))] = NA #or whatever else you want to do with the values that aren't 1 through 4`

Then if you really want bonus points you could coerce this back into a factor variable, but I find this is usually excessive.

data$column_to_recode = factor(data$column_to_recode,levels=c(0,1),ordered = TRUE)

I couldn't tell from your original question if the numeric codes were fine or if you wanted to use character strings instead, but the same logic applies, e.g:

data$column_to_recode[which(data$column_to_recode %in% c("(1) somewhat favorable","(2) somewhat unfavorable"))] = "Favorable"

should get you what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文