我想描述 R 中dt之后每个ID的三元组值N1
- N3
之间的关系:
id N1 N2 N3
1 3 2 1
2 3 2 2
3 3 4 1
4 2 3 2
5 1 2 3
6 2 2 4
7 2 2 2
8 2 1 3
我想创建一个新列,它给出了三个N列中值之间关系类型的分类,如下所示:
id N1 N2 N3 type1
1 3 2 1 A
2 3 2 2 A
3 3 4 1 B
4 2 3 2 B
5 1 2 3 C
6 2 2 4 C
7 2 2 2 D
8 2 1 3 E
类型A
适用于N1
> N2
和N3
,N2
>时的三元组N3
。
类型B
适用于N2
> N1
和N3
的三元组。
类型C
适用于N3
> N1
和N2
,N2
>时的三元组N1
当所有三个D
值相等时,类型N
适用于三元组。
类型E
适用于N2
< N1
和N3
答案 0 :(得分:3)
data.table
解决方案:
require(data.table)
dt <- data.table(id=1:8, N1=c(3,3,3,2,2,2,2,2), N2=c(2,2,4,3,1,2,2,1), N3=c(1,2,1,2,3,4,2,3))
dt[N1 > N2 & N1 > N3 & N2 > N3, type1 := 'A']
dt[N2 > N1 & N2 > N3, type1 := 'B']
dt[N3 > N1 & N3 > N2 & N2 > N1, type1 := 'C']
dt[N1 == N2 & N1 == N3, type1 := 'D']
dt[N2 < N1 & N2 < N3, type1 := 'E']
dt
id N1 N2 N3 type1
1: 1 3 2 1 A
2: 2 3 2 2 NA
3: 3 3 4 1 B
4: 4 2 3 2 B
5: 5 2 1 3 E
6: 6 2 2 4 NA
7: 7 2 2 2 D
8: 8 2 1 3 E
答案 1 :(得分:1)
您可以使用嵌套ifelse
语句执行此操作,使用with(dat, ...)
以避免重复输入dat$
:
dat$type1 = with(dat, ifelse(N1 > N2 & N1 > N3, "A",
ifelse(N2 > N1 & N2 > N3, "B",
ifelse(N3 > N1 & N3 > N2, "C",
ifelse(N1 == N2 & N1 == N3, "D",
ifelse(N2 < N1 & N2 < N3, "E", NA))))))
dat
# id N1 N2 N3 type1
# 1 1 3 2 1 A
# 2 2 3 2 2 A
# 3 3 3 4 1 B
# 4 4 2 3 2 B
# 5 5 2 1 3 C
# 6 6 2 2 4 C
# 7 7 2 2 2 D
# 8 8 2 1 3 C
数据:
dat = structure(list(id = 1:8, N1 = c(3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L
), N2 = c(2L, 2L, 4L, 3L, 1L, 2L, 2L, 1L), N3 = c(1L, 2L, 1L,
2L, 3L, 4L, 2L, 3L)), .Names = c("id", "N1", "N2", "N3"), row.names = c(NA,
-8L), class = "data.frame")
答案 2 :(得分:1)
使用dplyr
:
library(dplyr)
df %>%
mutate(type1 = ifelse(N1 > N2 & N1 > N3 & N2 > N3, "A",
ifelse(N2 > N1 & N2 > N3, "B",
ifelse(N3 > N1 & N3 > N2 & N2 > N1, "C",
ifelse(N1 == N2 & N1 == N3, "D",
ifelse(N2 < N1 & N2 < N3, "E", NA))))))
<强>输出强>
id N1 N2 N3 type1
1 1 3 2 1 A
2 2 3 2 2 <NA>
3 3 3 4 1 B
4 4 2 3 2 B
5 5 2 1 3 E
6 6 2 2 4 <NA>
7 7 2 2 2 D
8 8 2 1 3 E
数据
df <- structure(list(id = 1:8, N1 = c(3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L
), N2 = c(2L, 2L, 4L, 3L, 1L, 2L, 2L, 1L), N3 = c(1L, 2L, 1L, 2L, 3L, 4L, 2L, 3L)), .Names = c("id", "N1", "N2", "N3"), row.names = c(NA, -8L), class = "data.frame")