基于现有变量的值创建一个新的指标变量

3

I have a dataset like this:

> dput(head(BurnData))
structure(list(Treatment = c(0L, 0L, 0L, 0L, 0L, 0L), Gender = c(0L, 
0L, 0L, 0L, 0L, 0L), Race = c(0L, 1L, 1L, 0L, 1L, 1L), Surface = c(15L, 
20L, 15L, 20L, 70L, 20L), head = c(0L, 0L, 0L, 1L, 1L, 1L), buttock = c(0L, 
0L, 0L, 0L, 1L, 0L), trunk = c(1L, 1L, 0L, 1L, 1L, 1L), `upper leg` = c(1L, 
0L, 1L, 0L, 1L, 0L), `lower leg` = c(0L, 0L, 1L, 0L, 0L, 0L), 
    `respiratory tract` = c(0L, 0L, 0L, 0L, 0L, 0L), type = c(2L, 
    4L, 2L, 2L, 2L, 4L), `excision time` = c(12L, 9L, 13L, 11L, 
    28L, 11L), excision = c(0L, 0L, 0L, 1L, 1L, 0L), `antibiotic time` = c(12L, 
    9L, 13L, 29L, 31L, 11L), antibiotic = c(0L, 0L, 0L, 0L, 0L, 
    0L), infection_t = c(12L, 9L, 7L, 29L, 4L, 8L), infection = c(0L, 
    0L, 1L, 0L, 1L, 1L)), .Names = c("Treatment", "Gender", "Race", 
"Surface", "head", "buttock", "trunk", "upper leg", "lower leg", 
"respiratory tract", "type", "excision time", "excision", "antibiotic time", 
"antibiotic", "infection_t", "infection"), row.names = c(NA, 
6L), class = "data.frame")

我试图创建一个新变量,将指标headbuttocktrunkupper leglower legrespiratory tract合并为一个新的指标变量,其中0表示所有指标都为零,1表示仅有head2表示仅有buttock3...,7表示仅有respiratory tract8表示任意组合。

我一直在尝试使用mutatedplyr来实现这一点,但我做不好。我对此不是很擅长。

2个回答

3

以下是使用 R 的基本方法,使用 ifelse 语句。

ifelse(rowSums(d1[5:10]) > 1, 8, 
        ifelse(rowSums(d1[5:10]) == 0, 0, max.col(d1[5:10])))
#1 2 3 4 5 6 
#8 3 8 8 8 8 

0

你也可以尝试使用 tidyverse 中的 case_when

library(tidyverse)
d %>%
  select(head:`respiratory tract`) %>%
  mutate(res=case_when(rowSums(.) == 0 ~ 0,
                   rowSums(.) >  1 ~ 8,
                   head == 1 ~ 1,
                   buttock == 1 ~ 2, 
                   trunk == 1 ~ 3,
                   `upper leg`==1 ~ 4, 
                   `lower leg`==1~5, 
                   `respiratory tract`==1 ~ 6)) %>% 
  select(res) %>% 
  bind_cols(d,.)
  Treatment Gender Race Surface head buttock trunk upper leg lower leg respiratory tract type
1         0      0    0      15    0       0     1         1         0                 0    2
2         0      0    1      20    0       0     1         0         0                 0    4
3         0      0    1      15    0       0     0         1         1                 0    2
4         0      0    0      20    1       0     1         0         0                 0    2
5         0      0    1      70    1       1     1         1         0                 0    2
6         0      0    1      20    1       0     1         0         0                 0    4
  excision time excision antibiotic time antibiotic infection_t infection res
1            12        0              12          0          12         0   8
2             9        0               9          0           9         0   3
3            13        0              13          0           7         1   8
4            11        1              29          0          29         0   8
5            28        1              31          0           4         1   8
6            11        0              11          0           8         1   8

或者完全使用 Sotos 的优雅解决方案

 mutate(res=case_when(rowSums(.) == 0 ~ 0L,
                   rowSums(.) >  1 ~ 8L,
                   TRUE ~ max.col(.)))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接