Hi, In that case, Try: res1 <- do.call(rbind,lapply(years, function(x) c(as.numeric(x),rep(NA,max(sapply(years,length)-length(x)))))) A.K.
Hi, Thanks a lot for your answer. Each vector is a location, i.e. I want each vector in a different row. What I'd like to do is, in each vector, get the years in different columns, like this [1] [2] [3] [4] [1] 2004 2007 NA NA [2] 2010 2005 NA NA [3] 2009 2001 NA NA [4] 2006 2000 2004 2009 etc On Wednesday, January 8, 2014 11:32 AM, arun <smartpink...@yahoo.com> wrote: 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. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.