1

我有日期/时间信息,我想在其中获取按年份分组的“季节”中日期的平均值、最小值、最大值和范围,而我稍微成功的唯一方法是使用 tapply。最接近的是 tapply 中的汇总函数。结果是我只假设是按年份列出的最小值、最大值、第一个 Qu、中值等。这些不完全是我需要的,但工作正常。

我从 1 个赛季的 tapply 函数获得的结果示例:

$`2003`
                     Min.                   1st Qu.                    Median                      Mean                   3rd Qu.                      Max. 
"2003-04-22 00:00:00 UTC" "2003-05-03 00:00:00 UTC" "2003-05-12 00:00:00 UTC" "2003-05-10 02:00:00 UTC" "2003-05-18 00:00:00 UTC" "2003-05-21 00:00:00 UTC" 

$`2004`
                     Min.                   1st Qu.                    Median                      Mean                   3rd Qu.                      Max. 
"2004-04-07 00:00:00 UTC" "2004-04-13 00:00:00 UTC" "2004-05-10 00:00:00 UTC" "2004-05-01 07:08:56 UTC" "2004-05-11 12:00:00 UTC" "2004-05-20 00:00:00 UTC" 

我想要做的是将我为不同季节生成的这些列表组合成一个大的快乐数据框,我可以将其导出到 csv 中。我已经搜索和搜索,并且变得更加困惑。大多数人建议最简单的是

test = do.call(rbind, sSM, eSM, sC) #note here sSM, eSM, sC are my "seasons"

但是,这给了我一条错误消息或一个空的“测试”框架。

我已经阅读了 plyr 包并假设 ddply 而不是 tapply 将结果强制转换为数据框应该可以工作,但我无法弄清楚如何让它工作,因为我无法让它完成我的功能将需要诸如平均值、最小值、最大值和范围...

我想要的最终结果是这样的:

Season   Year   Min           Max      Mean        Median  
sSM     2003   2003-04-21   2003-5-1  2003-4-25   2003-4-23
eSM     2003...
sSM     2004...
eSM     2004...

那么我想做同样的事情,只在个人内部确定。我有一个 ID 字段,其中在某些年份测量了同一个人。我想在每个季节的年份中获得该人的平均开始日期。排除那些多年来没有被衡量的人。

由于我不了解如何操作列表和数组,甚至不了解 tapply 正在做什么,我不知道如何解决所有这些问题。我是否创建一个空数据框并将所有这些东西放入某个循环或其他东西中?以下是几年内 4 个“季节”的一些示例数据,请注意有些日期有日期和时间戳,有些只有日期。

    structure(list(Year = c(2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 2006L), ID = structure(c(11L, 
12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 14L, 15L, 1L, 2L, 3L, 6L), .Label = c("c_002", "c_102", 
"c_104", "c_105", "c_109", "c_401", "c_9814", "c_9815", "c_9816", 
"c_9819", "c_9901", "c_9902", "c_9905", "c_9908", "c_9911", "c_9912", 
"c_9916"), class = "factor"), sSM = structure(c(10L, 10L, 9L, 
17L, 8L, 8L, 7L, 18L, NA, 1L, NA, 15L, 13L, 12L, 6L, 3L, 5L, 
2L, 4L, 16L, 14L, 11L, 11L), .Label = c("04/07/2004 15:00", "04/23/2005 10:01", 
"04/25/2005 03:01", "04/27/2005 02:00", "04/27/2005 08:00", "04/29/2005 04:00", 
"05/01/2003", "05/03/2003", "05/04/2003", "05/05/2003", "05/05/2006", 
"05/07/2005 16:01", "05/11/2004 11:00", "05/11/2006", "05/13/2004 08:00", 
"05/14/2006", "05/17/2003", "05/17/2004 12:02"), class = "factor"), 
    eSM = structure(c(13L, 18L, 15L, 21L, 16L, 10L, 14L, 20L, 
    NA, 1L, NA, 11L, 7L, 5L, 6L, 17L, 3L, 2L, 4L, 19L, 9L, 12L, 
    8L), .Label = c("04/27/2004 05:00", "05/01/2005 05:00", "05/06/2005 05:00", 
    "05/08/2005 07:01", "05/12/2005 21:00", "05/15/2005 19:00", 
    "05/18/2004 13:00", "05/18/2006", "05/20/2006", "05/21/2003", 
    "05/21/2004 01:01", "05/23/2006", "05/24/2003", "05/25/2003", 
    "05/26/2003", "05/27/2003", "05/27/2005 01:00", "05/28/2003", 
    "05/28/2006", "06/01/2004 02:01", "06/03/2003"), class = "factor"), 
    sC = structure(c(9L, 12L, 16L, 19L, 18L, 12L, 7L, 13L, NA, 
    10L, NA, 20L, 4L, 14L, 11L, 5L, 1L, 2L, 3L, 8L, 17L, 6L, 
    15L), .Label = c("05/26/2005 00:00", "05/26/2005 10:00", 
    "05/27/2005 06:01", "05/28/2004 08:00", "05/29/2005 23:01", 
    "05/29/2006", "05/30/2003", "05/30/2006", "05/31/2003", "05/31/2004 06:01", 
    "05/31/2005 15:00", "06/01/2003", "06/01/2004 07:02", "06/01/2005 16:00", 
    "06/01/2006", "06/03/2003", "06/03/2006", "06/04/2003", "06/05/2003", 
    "06/05/2004 01:00"), class = "factor"), eC = structure(c(11L, 
    2L, 15L, 6L, 17L, 17L, 2L, 8L, NA, 7L, NA, 13L, 5L, 9L, 9L, 
    3L, 10L, 1L, 4L, 12L, 14L, 16L, 12L), .Label = c("06/03/2005 18:00", 
    "06/04/2003", "06/04/2005 04:01", "06/05/2005 05:01", "06/06/2004 22:00", 
    "06/07/2003", "06/07/2004 23:00", "06/08/2004 19:01", "06/08/2005 03:00", 
    "06/10/2005 20:00", "06/12/2003", "06/13/2006", "06/14/2004 00:00", 
    "06/14/2006", "06/16/2003", "06/18/2006", "06/19/2003"), class = "factor")), .Names = c("Year", 
"ID", "sSM", "eSM", "sC", "eC"), class = "data.frame", row.names = c(NA, 
-23L))

这是我迄今为止编写的一些代码:

dates$StartSM =  as.POSIXct(strptime(dates$sSM,"%m/%d/%Y",tz="UTC"),tz="UTC")                 
dates$EndSM =  as.POSIXct(strptime(dates$eSM,"%m/%d/%Y",tz="UTC"),tz="UTC")
dates$EndC =  as.POSIXct(strptime(dates$eC,"%m/%d/%Y %H:%M",tz="UTC"),tz="UTC")
dates$StartC =  as.POSIXct(strptime(dates$sC,"%m/%d/%Y %H:%M",tz="UTC"),tz="UTC")
sSpringM = tapply(dates$StartSM, dates$Year, summary)
eSpringM  = tapply(dates$EndSM, dates$Year, summary)
sCalving = as.vector(tapply(dates$StartC, dates$Year, summary))
eCalving = tapply(dates$EndC, dates$Year, summary)
datadates = do.call(rbind, sSpringM, eSpringM, sCalving)
4

1 回答 1

0

你有(无论如何在顶部)一个列表,称之为“你的列表”。试试这个来打印它们:

lapply(yourlist, format, "%Y-%m-%d")

如果您希望它们在矩阵中:

do.call(rbind, sapply(yourlist, unlist))

我在你的结构中看不到任何“冬天”、“春天”等。我过去曾使用 data.table 包的年、季度、月和周函数来返回年、季度、月和周的数值向量。使用整数年加上适当的小数加法(如年(.)+季度/4)很容易绘制。比相应的切割和标签麻烦更容易,否则会导致。

于 2011-08-04T00:02:43.403 回答