[英]How to create new variables based on multiple conditions on existing variable values
[英]create a new indicator variable based on values of existing variables
我有這樣的數據集:
> 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")
我正在嘗試創建一個新的變量,它將指示head
, buttock
, trunk
, upper leg
, lower leg
, respiratory tract
成一個新的指示變量,其中0
表示所有指標為零時, 1
- 僅head
,2 - 僅buttock
, 3
..., 7
- 僅respiratory tract
和8
- 任何一種的combination
。
我一直試圖用mutate
, dplyr
做這個,但我無法做對。 我不是很擅長這個。
這是使用ifelse
語句的基本R的方法。
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
你也可以嘗試使用case_when
的tidyverse
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(.)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.