[R] Converting list with different vector lengths into a dataframe (or export it as .csv)

arun smartpink111 at yahoo.com
Wed Jan 8 17:32:43 CET 2014


Hi,

It is not clear about the final output.

You may try:
uniqYrs <- sort(as.numeric(unique(unlist(years))))
res <-t(sapply(years, function(x) uniqYrs %in% as.numeric(x)))*1
colnames(res) <- uniqYrs
 head(res)
#  2000 2001 2003 2004 2005 2006 2007 2008 2009 2010 2011
#1    0    0    0    1    0    0    1    0    0    0    0
#2    0    0    0    0    1    0    0    0    0    1    0
#3    0    1    0    0    0    0    0    0    1    0    0
#4    1    0    0    1    0    1    0    0    1    0    0
#5    1    0    0    0    0    1    0    0    0    0    0
#6    1    0    0    0    0    1    0    0    0    0    0


A.K.



Dear all, 

I have this list 

years <- list(c("2004", "2007"), c("2010", "2005"), c("2009", "2001"), 
    c("2006", "2000", "2004", "2009"), c("2006", "2000"), c("2006", "2000"), 
    c("2005", "2007"), c("2005", "2007"), c("2001", "2006"), 
    c("2005", "2001", "2008"), c("2005", "2001", "2008"), c("2005", 
    "2000"), c("2005", "2000"), c("2006", "2005", "2003"), c("2006", 
    "2005", "2003"), c("2007", "2004", "2007"), c("2004", "2007" 
    ), c("2004", "2007"), c("2004", "2007"), c("2010", "2007" 
    ), c("2008", "2005"), c("2008", "2005"), c("2008", "2005" 
    ), c("2004", "2007"), c("2004", "2007"), c("2011", "2006" 
    ), c("2009", "2005", "2003"), c("2009", "2005", "2003"), 
    c("2009", "2001"), c("2007", "2003", "2004"), c("2007", "2004" 
    ), c("2007", "2004"), c("2008", "2010", "2001"), c("2010", 
    "2005"), c("2007", "2005"), c("2007", "2005"), c("2007", 
    "2005"), c("2010", "2005"), c("2010", "2005"), c("2010", 
    "2005"), c("2007", "2005"), c("2007", "2005"), c("2008", 
    "2007"), c("2003", "2005"), c("2003", "2005"), c("2005", 
    "2003"), c("2005", "2003"), c("2005", "2003")) 

Each vector represents an location, and the years that this 
location has burned. Therefore, I'd like to get a dataframe with each 
vector on a row, and the years in columns, but I can't because vectors 
have different lengths. 
I have also tried to export it to a csv (and then open it with excel), but I get the same error. 

Thank a lot in advance for your valuable help. 





More information about the R-help mailing list