如何从R中的数据框中选择具有特定日期的某些行

时间:2014-07-29 20:36:30

标签: r dataframe rows

我有一个大型数据集,我想特别挑出一些行,我想知道是否有人可以帮我这个?非常感谢你的帮助!!

例如,如果我只想挑选来自2/1 / 2008-5 / 1/2008加上9/1 / 2008-11 / 1/2008的行,我该怎么做? 非常感谢!!

有人可以帮忙吗?

date    mpressure   mxtemp
2008-01-01  1025.3  15.7
2008-01-02  1025.6  16.0    <   
2008-01-03  1023.6  18.1    <
2008-01-04  1021.8  18.4    <
2008-01-05  1020.1  20.9    <
2008-01-06  1019.7  20.7
2008-01-07  1018.4  24.0
2008-01-08  1016.7  23.7
2008-01-09  1015.3  24.5    <
2008-01-10  1014.3  21.8    <
2008-01-11  1012.9  23.4    <

然后我会得到这样的东西?

date    mpressure   mxtemp
2008-01-02  1025.6  16.0    <   
2008-01-03  1023.6  18.1    <
2008-01-04  1021.8  18.4    <
2008-01-05  1020.1  20.9    <
2008-01-09  1015.3  24.5    <
2008-01-10  1014.3  21.8    <
2008-01-11  1012.9  23.4    <

1 个答案:

答案 0 :(得分:6)

将您的日期列转换为Date - 输入:

df$date <- as.Date(df$date)

然后根据您的规格进行分组:

with(df, df[(date >= "2008-01-02" & date <= "2008-01-05") | 
                                 (date >= "2008-01-09" & date <= "2008-01-11"), ])
#         date mpressure mxtemp
#2  2008-01-02    1025.6   16.0
#3  2008-01-03    1023.6   18.1
#4  2008-01-04    1021.8   18.4
#5  2008-01-05    1020.1   20.9
#9  2008-01-09    1015.3   24.5
#10 2008-01-10    1014.3   21.8
#11 2008-01-11    1012.9   23.4