R:根据日期循环遍历数据帧以提取数据子集 [英] R: loop through data frame extracting subset of data depending on date
问题描述
我有一个大型数据框,其中包含如下所示的数据:
I have a large data frame that consists of data that looks something like this:
date w x y z region
1 2012 01 21 43 12 3 NORTH
2 2012 02 32 54 21 16 NORTH
3 2012 03 14 32 65 32 NORTH
4 2012 04 65 33 75 21 NORTH
: : : : : : :
: : : : : : :
12 2012 12 32 58 53 17 NORTH
13 2012 01 12 47 43 23 SOUTH
14 2012 02 87 43 21 76 SOUTH
: : : : : : :
25 2012 01 12 46 84 29 EAST
26 2012 02 85 29 90 12 EAST
: : : : : : :
: : : : : : :
我要提取具有相同数据的部分 date
的值,例如仅在 2012 01
中执行此操作,我将创建一个子数据集
I want to extract section of the data that have the same date
value, for example to do this just for 2012 01
I would just create a subset of data
data_1 <- subset(data, date == "2012 01")
,这为我提供了 2012 01
的所有数据,将功能应用于此数据。我希望能够将我的功能应用于数据的所有可能子集,因此理想情况下,我将遍历大型数据框并提取 2012 01,2012 02,2012 03,2012的数据04 ...
并将函数分别应用于这些数据子集。
and this gives me all the data for 2012 01
but I then go on to apply a function to this data. I would like to be able to apply my function to all possible subsets of my data, so ideally I would be looping through my large data frame and extracting the data for 2012 01, 2012 02, 2012 03, 2012 04...
and applying a function to each of these subsets of data separately.
但是,即使我的数据帧长度发生变化,我也希望能够将此应用于我的数据帧,因此它不一定总是从 2012 01-2012 12
,日期范围可能会有所不同,因此有时可能会用于例如 2011 03-2013 01
。
But I would like to be able to apply this to my data frame even if my data frames length were to change, so it may not always go from 2012 01 - 2012 12
, the range of dates may vary so that sometimes it may be used on data from for example 2011 03 - 2013 01
.
推荐答案
这是您想要的吗?
df_list<-split(data,as.factor(data $ date))
这篇关于R:根据日期循环遍历数据帧以提取数据子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!