[R] Creating the right table from lapply list

MacQueen, Don macqueen1 at llnl.gov
Thu Mar 29 02:18:21 CEST 2018


Perhaps this toy example will help:

## example data
output <- list(1:5, 1:7, 1:4)

lens <- lapply(output, length)
maxlen <- max(unlist(lens))
outputmod <- lapply(output, function(x, maxl) c(x, rep(NA, maxl-length(x))), maxl=maxlen)
outputmat <- do.call(cbind, outputmod)
write.csv(outputmat, na='')

The idea is to pad the shorter vectors with NA (missing) before converting to a matrix structure.

I don't really need to know where the data came from, or that it's ncdf data, or how many months or years, etc. But I do need to know the structure of your "output" list. I'm assuming each element is a simple vector of numbers, and that the vectors' lengths are not all the same. If that's correct, then my example may be what you need.

This uses only base R methods, which I generally prefer. And no doubt it can be done more cleverly, or in a way that needs fewer intermediate variables ... but I don't really care.

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 
On 3/28/18, 9:32 AM, "R-help on behalf of orlin mitov via R-help" <r-help-bounces at r-project.org on behalf of r-help at r-project.org> wrote:

    Hello,
      I have no previous experience with R, but had to learn on the fly in the past couple of weeks. Basically, what I am trying to do is read a certain variable from a series of files and save it as csv-table. The variable has an hourly value for each month in a year for the past 20 years and has to be read for different geographical locations. So there will be 12 files per year (1 for each month) and the values for the variable from each file will be 696 to 744 (depending on how many days x 24 hours there were in the month).What I achieved is to to read the values from all 12 files stored in directory with a function and add them as vectors to a lapply-list:
    
    
    
    Myfunction <- function(filename) {
     nc <- nc_open(filename)
     lon <- ncvar_get(nc, "lon")
     lat <- ncvar_get(nc, "lat")
     RW <- ncvar_get(nc, "X")
     HW <- ncvar_get(nc, "Y")
     pt.geo <- c(8.6810 , 50.1143)
     dist <- sqrt( (lon - pt.geo[1])^2 + (lat - pt.geo[2])^2 )
     ind <- which(dist==min(dist, na.rm=TRUE),arr.ind=TRUE)
     sis <- ncvar_get(nc, "SIS", start=c(ind[1],ind[2],1), count=c(1,1,-1))
     vec <- c(sis)
    }
    
    filenames <- list.files(path = "C:/Users/Desktop/VD/Solardaten/NC", pattern = "nc", full.names = TRUE)
     output <- lapply(filenames, Myfunction)
    
    
    
    And here start my problems with saving "output" as a csv table. Output would contain 12 vectors of different lenght.I want to have them as 12 columns (1x per month) in Excel and each column should have as many row-entries as there are values for this month.Whatever I tried with write.table I was not able to achieve this (tried converting the output to a matrix, also no successes).Please help! Or should I be trying to have the 12 elements as data frames and not vectors?
    This is how I want the table for each year to look - 12 columns and all the respective values in the rows (column names I can add by myself):
    Best regardsOrlin
    
    



More information about the R-help mailing list