使用条件选择来对数据集中的多个变量进行子集化

时间:2018-02-16 03:31:02

标签: r

我需要使用条件选择来创建数据的子集,其中包含来自控制组的记录,权重> 440,DMI> 13.我还需要在这个数据子集中找到记录数量最多的牛品种。

我试过的代码是:

> dat2[dat2$Treatment == "control" & dat2$Weight>440 & dat2$DMI >13]

但我不确定这是否正确。

> dput(head(dat2))
structure(list(Animal = 1:6, Weight = c(455.96, 418.05, 436.31, 
448.22, 418.35, 467.78), DMI = c(14.81, 17.63, 17.81, 15.01, 
15.42, 12.58), Breed = structure(c(3L, 3L, 2L, 2L, 3L, 3L), .Label = 
c("Angus", 
"Brahman", "Hereford", "Nelore"), class = "factor"), Treatment = 
structure(c(1L, 
3L, 1L, 1L, 3L, 1L), .Label = c("Control", "High", "Low"), class = ]
"factor"), 
Sex = structure(c(1L, 1L, 2L, 2L, 1L, 1L), .Label = c("Castrate", 
"Female", "Male"), class = "factor")), .Names = c("Animal", 
"Weight", "DMI", "Breed", "Treatment", "Sex"), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

使用wheight和dmi提取控件,正如@akrun所指出的那样:

subset <- dat2[dat2$Treatment == "Control" & dat2$Weight>440 & dat2$DMI >13,]

要获得具有最多出​​现次数的牛品种,您可以使用table

table(subset$Breed)