[R] Creating the right table from lapply list

orlin mitov orlin_mitov at yahoo.com
Wed Mar 28 18:32:33 CEST 2018

  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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: grafik.png
Type: image/png
Size: 120645 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20180328/424b655c/attachment.png>

More information about the R-help mailing list