在R中创建一个新列,查看前两列中的数据并指定因子级别?

在R中创建一个新列,查看前两列中的数据并指定因子级别?,r,R,我有一个关于蜗牛两种寄生虫的数据集,我想创建一个新的列,说明蜗牛是否没有寄生虫“None”、1型“TYPE1”、2型“TYPE2”或两种寄生虫“DUAL” 我曾想过使用if或if-else,但我似乎无法使其正常工作?您可以使用一些if-else语句,但我认为这更容易 dat <- read.table(header = TRUE, text="row Block Weight Parasite1 Parasite2 1 1 1.23 1 1

我有一个关于蜗牛两种寄生虫的数据集,我想创建一个新的列,说明蜗牛是否没有寄生虫“None”、1型“TYPE1”、2型“TYPE2”或两种寄生虫“DUAL”


我曾想过使用if或if-else,但我似乎无法使其正常工作?

您可以使用一些if-else语句,但我认为这更容易

dat <- read.table(header = TRUE, text="row Block Weight Parasite1  Parasite2
1      1   1.23        1           1         
2      1   3.14        1           1         
3      1   2.55        1           0         
4      1   2.67        0           1         
5      1   3.36        0           1         
6      1   3.16        0           0         
7      1   3.41        0           1         
8      1   2.47        0           1         
9      1   1.56        0           1         
10     1   2.66        1           1")[,-1]


within(dat, {
  type <- interaction(Parasite1, Parasite2, sep = '')
  type <- factor(type, levels = c('11','10','01','00'),
                 labels = c('DUAL','TYPE1','TYPE2','None'))
})

#    Block Weight Parasite1 Parasite2  type
# 1      1   1.23         1         1  DUAL
# 2      1   3.14         1         1  DUAL
# 3      1   2.55         1         0 TYPE1
# 4      1   2.67         0         1 TYPE2
# 5      1   3.36         0         1 TYPE2
# 6      1   3.16         0         0  None
# 7      1   3.41         0         1 TYPE2
# 8      1   2.47         0         1 TYPE2
# 9      1   1.56         0         1 TYPE2
# 10     1   2.66         1         1  DUAL
dat你可以试试

df$NewCol <- c('None', 'TYPE1', 'TYPE2', 'DUAL')[with(df,
                   as.numeric(factor(1+2*Parasite1+4*Parasite2)))]

df
#   Block Weight Parasite1 Parasite2 NewCol
#1      1   1.23         1         1   DUAL
#2      1   3.14         1         1   DUAL
#3      1   2.55         1         0  TYPE1
#4      1   2.67         0         1  TYPE2
#5      1   3.36         0         1  TYPE2
#6      1   3.16         0         0   None
#7      1   3.41         0         1  TYPE2
#8      1   2.47         0         1  TYPE2
#9      1   1.56         0         1  TYPE2
#10     1   2.66         1         1   DUAL
df$NewCol
df$NewCol <- c('None', 'TYPE1', 'TYPE2', 'DUAL')[with(df,
                   as.numeric(factor(1+2*Parasite1+4*Parasite2)))]

df
#   Block Weight Parasite1 Parasite2 NewCol
#1      1   1.23         1         1   DUAL
#2      1   3.14         1         1   DUAL
#3      1   2.55         1         0  TYPE1
#4      1   2.67         0         1  TYPE2
#5      1   3.36         0         1  TYPE2
#6      1   3.16         0         0   None
#7      1   3.41         0         1  TYPE2
#8      1   2.47         0         1  TYPE2
#9      1   1.56         0         1  TYPE2
#10     1   2.66         1         1   DUAL
 df <- structure(list(Block = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
 1L), Weight = c(1.23, 3.14, 2.55, 2.67, 3.36, 3.16, 3.41, 2.47, 
 1.56, 2.66), Parasite1 = c(1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
 1L), Parasite2 = c(1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L)), .Names = c("Block", 
 "Weight", "Parasite1", "Parasite2"), class = "data.frame", row.names = c("1", 
 "2", "3", "4", "5", "6", "7", "8", "9", "10"))