我正在尝试使用ddply
with在数据框中transform
填充一个新变量 ( summary_Date
),其中包含变量ID
和Date
. 变量的值是根据正在评估的片段的长度选择的ifelse
:
如果在给定月份中对 ID 的观察少于五个,我希望summary_Date
通过将日期四舍五入到最近的月份来计算(使用round_date
from package lubridate
);如果在给定的月份中有超过五个 ID 的观察值,我希望它summary_Date
只是Date
.
require(plyr)
require(lubridate)
test.df <- structure(
list(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1
, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2)
, Date = structure(c(-247320000, -246196800, -245073600, -243864000
, -242654400, -241444800, -126273600, -123595200
, -121176000, -118497600, 1359385200, 1359388800
, 1359392400, 1359396000, 1359399600, 1359403200
, 1359406800, 1359410400, 1359414000, 1359417600
, 55598400, 56116800, 58881600, 62078400, 64756800
, 67348800, 69854400, 72964800, 76161600, 79012800
, 1358589600, 1358676000, 1358762400, 1358848800
, 1358935200, 1359021600, 1359108000, 1359194400
, 1359280800, 1359367200), tzone = "GMT"
, class = c("POSIXct", "POSIXt"))
, Val=rnorm(40))
, .Names = c("ID", "Date", "Val"), row.names = c(NA, 40L)
, class = "data.frame")
test.df <- ddply(test.df, .(ID, floor_date(Date, "month")), transform
, summary_Date=as.POSIXct(ifelse(length(ID)<5
, round_date(Date, "month")
,Date)
, origin="1970-01-01 00:00.00"
, tz="GMT")
# Included length_x to easily see the length of the subset
, length_x = length(ID))
head(test.df,5)
# floor_date(Date, "month") ID Date Val summary_Date length_x
# 1 1962-03-01 1 1962-03-01 12:00:00 -0.1037988 1962-03-01 3
# 2 1962-03-01 1 1962-03-14 12:00:00 0.2923056 1962-03-01 3
# 3 1962-03-01 1 1962-03-27 12:00:00 0.4435410 1962-03-01 3
# 4 1962-04-01 1 1962-04-10 12:00:00 0.1159164 1962-04-01 2
# 5 1962-04-01 1 1962-04-24 12:00:00 2.9824075 1962-04-01 2
该ifelse
语句似乎有效,但“summary_Date”中的值似乎是为转换正在处理的子集计算的第一个值,而不是特定于行的值。例如在第 3 行,summary_Date
应该是1962-04-01
因为日期1962-03-27 12:00:00'
应该向上取整(因为子集中的行少于五行),而是在该子集中的所有行中重复summary_Date
( ) 的第一个计算值。1962-03-01
编辑:我受到里卡多的回答的启发,data.table
使用ddply
. 它也可以:
test.df <- ddply(test.df, .(ID, floor_date(Date, "month")), transform
, length_x = length(ID))
test.df <- ddply(test.df, .(ID, floor_date(Date, "month")), transform
, summary_Date=as.POSIXct(ifelse(length_x<5
, round_date(Date, "month")
,Date)
, origin="1970-01-01 00:00.00"
, tz="GMT"))
head(test.df,5)[c(1,3:7)]
# floor_date(Date, "month") ID Date Val length_x summary_Date
# 1 1962-03-01 1 1962-03-01 12:00:00 -0.1711212 3 1962-03-01
# 2 1962-03-01 1 1962-03-14 12:00:00 -0.1531571 3 1962-03-01
# 3 1962-03-01 1 1962-03-27 12:00:00 0.1256238 3 1962-04-01
# 4 1962-04-01 1 1962-04-10 12:00:00 1.4481225 2 1962-04-01
# 5 1962-04-01 1 1962-04-24 12:00:00 -0.6508731 2 1962-05-01