如何从R中的数据框架中选择特定日期的某些行 [英] How to select some rows with specific date from a data frame in R
本文介绍了如何从R中的数据框架中选择特定日期的某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果我只想从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<
解决方案
将日期列转换为日期
-type:
df $ date< - as.Date(df $ date)
然后根据您的规格进行子集:
<$ p (df,df [(date> =2008-01-02&=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
I have a large dataset and I want to pick out some of the rows particularly, I am wondering if anyone could help me with this? Thank you so much for your help!!
For example, if I just want pick out rows that are from 2/1/2008-5/1/2008 plus 9/1/2008-11/1/2008, how can I do that? Thank you very much!!
Can anyone please help?
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 <
And then I will get something like this?
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 <
解决方案
Convert your date column to Date
-type:
df$date <- as.Date(df$date)
Then subset according to your specifications:
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
这篇关于如何从R中的数据框架中选择特定日期的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文