我有一个庞大的数据集,我有一个名为season的专栏。有4个季节,即冬季,春季,夏季和秋季。
Region Year Male Female Area DATE Day Month Season
WEST 1996 0 1 4 06-04-96 Saturday April Spring
EAST 1996 0 1 16 29-06-96 Saturday June Summer
WEST 1996 0 1 4 19-10-96 Saturday October Winter
WEST 1996 0 1 4 20-10-96 Sunday October Winter
EAST 1996 0 1 16 01-11-96 Friday November Winter
EAST 1996 0 1 16 11-11-96 Monday November Winter
WEST 1996 0 1 4 19-11-96 Tuesday November Winter
WEST 1996 0 1 4 28-11-96 Thursday November Winter
WEST 1996 0 1 4 10-12-96 Tuesday December Winter
WEST 1997 0 1 4 17-01-97 Friday January Winter
WEST 1997 0 1 4 28-03-97 Friday March Spring
所以我试图创建一个子集,我希望R向我显示季节为冬季和秋季的条目。
我在我想要的部分中首先创建了一个子集。
secondphase<-subset(eb1, Area>16)
现在从这个子集中,我想要季节是冬季和秋季。
我尝试了这些代码 -
th2<-subset(secondphase, Season== "Winter")
th3<-subset(secondphase, Season=="Autumn")
现在有没有办法合并这两个子集?或创建一个子集,我可以选择我想要区域> 16的条件,季节应该是冬季和秋季。
感谢您的帮助。
答案 0 :(得分:1)
您也可以使用带有过滤功能的dplyr包
filter(secondphase, grepl("Winter|Autumn", Season))
答案 1 :(得分:0)
方法1
my_subset <- eb1[eb1$Season %in% c("Winter", "Autumn") & eb1$Area > 16,]
方法2
th2 <- subset(secondphase, Season== "Winter")
th3 <- subset(secondphase, Season=="Autumn")
final <- rbind(th2, th3)
方法3
final <-subset(eb1[eb1$Area > 16,], Season== "Winter" | Season=="Autumn")
答案 2 :(得分:0)
使用data.table
方法,
library("data.table")
DT<-data.table(eb1)
subsetDT<-subset(DT, Season %in% c("Autmn","Winter") & Area > 16)
完成这项工作。