从不同数量的行的lapply函数创建数据框 [英] Creating a dataframe from an lapply function with different numbers of rows

查看:166
本文介绍了从不同数量的行的lapply函数创建数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期列表(df2)和一个单独的数据框架,每周日期和当天的测量值(df1)。我需要的是在采样日期(df2)之前的一年内输出数据帧,并用此进行测量。

I have a list of dates (df2) and a separate data frame with weekly dates and a measurement on that day (df1). What I need is to output a data frame within a year prior to the sample dates (df2) and the measurements with this.

eg1 <- data.frame(Date=seq(as.Date("2008-12-30"), as.Date("2012-01-04"), by="weeks"))
eg2 <- as.data.frame(matrix(sample(0:1000, 79*2, replace=TRUE), ncol=1))
df1 <- cbind(eg1,eg2)
df2 <- as.Date(c("2011-07-04","2010-07-28"))

我以前曾问过一个类似的问题(从基于日期的一个数据帧输出各种子集)被有效地应用于每日数据(哪里有平衡数量的行)通过这个功能...

A similar question I have previously asked (Outputting various subsets from one data frame based on dates) was answered effectively with daily data (where there is a balanced number of rows) through this function...

output <- as.data.frame(lapply(df2, function(x) {
  df1[difftime(df1[,1], x - days(365)) >= 0 & difftime(df1[,1], x) <= 0, ]
}))

然而,每周数据的行数不均匀意味着这是不可能的。当as.data.frame功能被删除时,代码工作,但我得到一个数据帧列表。 我想做的是将NA的一行添加到包含较少观察值的数据帧中,以便我可以输出一个数据帧,以便我可以应用忽略NA值的功能,例如..

However, with weekly data an uneven number of rows means this is not possible. When the 'as.data.frame' function is removed, the code works but I get a list of data frames. What I would like to do is append a row of NA's to those dataframes containing fewer observations so that I can output one dataframe, so that I can apply functions simply ignoring the NA values e.g...

df2 <- as.Date(c("2011-01-04","2010-07-28"))
output <- as.data.frame(lapply(df2, function(x) {
df1[difftime(df1[,1], x - days(365)) >= 0 & difftime(df1[,1], x) <= 0, ]
}))
col <- c(2,4)
output_two <- output[,col]
Mean <- as.data.frame(apply(output_two,2,mean), na.rm = TRUE)


推荐答案

尝试

 lst <- lapply(df2, function(x) {df1[difftime(df1[,1], x - days(365)) >= 0 & 
                difftime(df1[,1], x) <= 0, ]})
  n1 <- max(sapply(lst, nrow))
  output <- data.frame(lapply(lst,  function(x) x[seq_len(n1),]))

这篇关于从不同数量的行的lapply函数创建数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆