如何将行保留在分类数据中?

我有一个动物收容所的数据集,其中“繁殖”参数有50多种不同的可能性。我查看了数据汇总,共有4个品种占主导地位。我的问题是,如何创建仅包含这四个品种的数据集(并使所有其他参数保持相同)?

Here's what I tried so far: (meow2 is the original data)

meow3 <- meow2[ which(meow2$breed1=="domestic shorthair" & "domestic mediumhair" & "domestic longhair" & "siamese"),]

一些在线研究建议我创建一个子集?这是我的尝试:

meow3 <- subset(meow2, breed1=="domestic shorthair" "domestic meduimhair" "domestic longhair" "siamese")

我确定确实存在一些格式问题,但是我确实很难为此找到在线资源。我也尝试调查错误,但是似乎没有任何效果。

评论
kautem
kautem

The simplest way to do this is with %in%:

common_breeds <- c("domestic shorthair","domestic mediumhair",
          "domestic longhair", "siamese")   
meow3 <- subset(meow2, breed1 %in% common_breeds)

你可以做类似的事情

 ... breed1=="domestic shorthair" | breed1=="domestic mediumhair" |
     breed1=="domestic longhair" | breed1=="siamese" ...

(you need to use | (or) rather than & (and) ...)

点赞
评论