简体   繁体   English

子集R中的两个因子

[英]Subsetting two factors in R

I have a huge dataset, and I have a column called season. 我有一个庞大的数据集,并且有一列称为Season。 There are 4 seasons ie Winter, Spring, Summer and Autumn. 有四个季节,即冬季,春季,夏季和秋季。

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

So I am trying to create a subset where I want R to show me entries with season as Winter and Autumn. 因此,我尝试创建一个子集,在该子集中,我希望R向我显示冬季和秋季作为季节的条目。

I created a subset first of the portion I want. 我首先创建了想要的部分的子集。

secondphase<-subset(eb1, Area>16)

now from this subset, I want where Season is Winter and Autumn. 现在从这个子集中,我想要季节是冬季和秋季。

I tried these codes- 我尝试了这些代码-

th2<-subset(secondphase, Season== "Winter")
th3<-subset(secondphase, Season=="Autumn")

Now is there a way to merge these two subsets? 现在有没有办法合并这两个子集? or create a subset where I can select the conditions where I want area>16, season should be Winter and autumn. 或创建一个子集,在该子集中我可以选择希望区域> 16的条件,季节应该是冬季和秋季。

Thanks for the Help. 谢谢您的帮助。

您还可以将dplyr软件包与filter函数一起使用

filter(secondphase, grepl("Winter|Autumn", Season))

Method 1 方法1

my_subset <- eb1[eb1$Season %in% c("Winter", "Autumn") & eb1$Area > 16,]

Method 2 方法2

th2   <- subset(secondphase, Season== "Winter")
th3   <- subset(secondphase, Season=="Autumn")
final <- rbind(th2, th3)

Method 3 方法3

final <-subset(eb1[eb1$Area > 16,], Season== "Winter" | Season=="Autumn")

With a data.table approach, 使用data.table方法

library("data.table")
DT<-data.table(eb1)
subsetDT<-subset(DT, Season %in% c("Autmn","Winter") & Area > 16)

does the job. 做这份工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM