使用多个where / between语句创建数据子集

时间:2016-10-09 13:35:34

标签: r

我有一个数据集,包括2个不同月份和相同时间段的2天。它显示了在这段时间内有多少人占据了房屋。我想按日期,时间段和家庭分开数据。

所以我想得到日期是01-02-2010的所有记录,时间段14:00:00 - 19:00:00,其中houseid是N60421A。目前,data.type存储为除了数字占用者之外的字符。

http://www.sharecsv.com/s/aa6d4dc34acfbaf73ada1d2c8764b888/modecsv.csv

Atm我试过这个,但似乎没有结果

data2 = subset(data, dayMonthYear == "01/02/2010" && Houses == "N60421A")

在SQL中我会做类似

的事情
SELECT *
From data
where dayMonthYear == "01/02/2010"
AND houses == "N60421A"
AND time > 14:00:00
AND time < 19:00:00

1 个答案:

答案 0 :(得分:0)

这对你有用......

#Combine date and time into a new POSIXct variable "Time1"
data$Time1 <- as.POSIXct(paste(data$dayMonthYear, data$Time), format="%d/%m/%Y %H:%M:%S")

#Subset 
data2 <-subset(data, dayMonthYear == "01/02/2010" & Houses == "N60421A" & strftime(Time1, "%H") %in% c('14','15','16','17','18','19'))

你也可以使用&#34; chron&#34;包和标准R子集......

#Approach 2
#Load Library
library(chron)

#Convert Time from factor while creating new variable "Time2"
data$Time2 <- chron(times = as.character(data$Time))

#Subset
data2 <- data[(data$dayMonthYear == "01/02/2010" & data$Houses == "N60421A" & data$Time2 >= "14:00:00" & data$Time2 <= "19:00:00" ),]