如何选择R中的每5个其他观测值?

时间:2016-05-26 11:20:03

标签: r select

我有一个包含10"房屋的数据集"每天的每一分钟都有能源生产。像这样:

HouseID Time KwH
1       1    X
2       1    X
3       1    X
4       1    X
5       1    X
6       1    X
7       1    X
8       1    X
9       1    X
10      1    X
1       2    X
2       2    X
3       2    X
4       2    X
5       2    X
6       2    X
7       2    X
8       2    X
9       2    X
10      2    X

我想删除带有houseIDs 6到10的行,这样我只剩下houseID 1,2,3,4和5的观察结果。

2 个答案:

答案 0 :(得分:2)

你可以尝试

newdf <- df1[!df1$HouseID %in% 6:10,]
#   HouseID Time KwH
#1        1    1   X
#2        2    1   X
#3        3    1   X
#4        4    1   X
#5        5    1   X
#11       1    2   X
#12       2    2   X
#13       3    2   X
#14       4    2   X
#15       5    2   X

数据

df1 <- structure(list(HouseID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
      10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Time = c(1L, 1L, 
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
       2L, 2L), KwH = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
       1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "X", 
       class = "factor")), .Names = c("HouseID", "Time", "KwH"), 
       class = "data.frame", row.names = c(NA, -20L))

答案 1 :(得分:-1)

假设df是数据框的名称,那么只需使用以下内容:

df2 <- subset(df, df$HouseID==1:5)