78440

Creating a dataframe from an lapply function with different numbers of rows

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, ] }))

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. <strong>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</strong>, 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)

Answer1:

Try

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),]))

Recommend

  • How to identify animations completed or not? [closed]
  • How to access the structure and get the column list ,datatypes of refcursor?
  • passing string to ggplot function
  • plot phylogenetic logistic regression with binary response variable
  • I want to use Stormpath, Express.js, and Node.js. Do I want stormpath-express or express-stormpath?
  • Can you use the lapply() function to alter the value of input?
  • R Tree With n Branches
  • Android multuple camera preview
  • string Multiplication in C++
  • Calculating subtotals in R
  • Count the number of non-null values in a Spark DataFrame
  • Combine two small queries (that group by different values) into one query
  • Calculate area of cross-section as function of height
  • Direct insert data in mysql data in android
  • LyX — Title, Abstract, Keywords Missing When Using Elsevier Class in View
  • Converting float[,] to list in f#?
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Extract data between rows r
  • 3D Math: Calculate Bank (Roll) angle from Look and Up orthogonal vectors
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • Getting errors while using neuralnet function
  • Refactoring advice: maps to POJOs
  • Not able to display correct data in table -AngularJS
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • quiver not drawing arrows just lots of blue, matlab
  • SAXReader not re-ecape characters
  • how to avoid repetitive constructor in children
  • Checking free space on FTP server
  • Spark fat jar to run multiple versions on YARN
  • TFS: Get latest causes slow project reloading
  • ActionScript 2 vs ActionScript 3 performance
  • How do I use the BLAS library provided by MATLAB?
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • json Serialization in asp
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?