r - Subsetting a large list and recombining into a dataframe -
say have list in r has lot of elements. structured example below (symbolx 1999, symbolx 2000, symboly 1999, symboly 2000, etc). trying each symbol put years (rbind) , entire set of symbols put newly combined years 1 dataframe (cbind).
right code below seems pretty slow when on list of 2600 elements. suggest more efficient way accomplish this?
i made reproducible example below (the actual values not important, random now).
symbols <- c("symbolx","symboly") first <- structure(list(`_id` = "123182914914", symbol = "symbolx", year = 1999, monthly = data.frame("col2"=runif(10, -0.03, 0.03))), .names = c("_id", "symbol", "year", "monthly")) second <-structure(list(`_id` = "123182914915", symbol = "symbolx", year = 2000, monthly = data.frame("col2"=runif(10, -0.03, 0.03))), .names = c("_id", "symbol", "year", "monthly")) third <- structure(list(`_id` = "123182914916", symbol = "symboly", year = 1999, monthly = data.frame("col2"=runif(10, -0.03, 0.03))), .names = c("_id", "symbol", "year", "monthly")) fourth <- structure(list(`_id` = "123182914917", symbol = "symboly", year = 2000, monthly = data.frame("col2"=runif(10, -0.03, 0.03))), .names = c("_id", "symbol", "year", "monthly")) big_list <- list(first,second,third,fourth) unique_years <- unique(unlist(lapply(big_list, '[[', 'year'))) all_data <- vector(mode = "list", length = length(symbols)) (i in 1:length(symbols)) { xts_data <- do.call("rbind", lapply(big_list[((length(unique_years)*(i-1))+1):(length(unique_years)*i)], '[[', 'monthly')) colnames(xts_data) <- symbols[i] all_data[[i]] <- xts_data } # create final output combined dataframe master_data <- do.call("cbind", lapply(all_data, function(x) x))
Comments
Post a Comment