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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Olivereta
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.

Reply | Threaded
Open this post in threaded view
|

Re: Converting list with different vector lengths into a dataframe (or export it as .csv)

arun kirshna
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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting list with different vector lengths into a dataframe (or export it as .csv)

arun kirshna
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 <[hidden email]> 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting list with different vector lengths into a dataframe (or export it as .csv)

Olivereta
In reply to this post by arun kirshna
Thanks a lot for your answer.
What I really want is a data.frame like this (I am just building it myself):

years

    [1]         [2]       [3]    [4]
[1]  2004      2007     NA    NA
[2]  2010     2005      NA    NA
[3]  2009      2001     NA    NA
[4] 2006      2000       2004   2009
[5]2006        2000      NA    NA
etc


Thank you very much, and apologies for not being clear enough.

Reply | Threaded
Open this post in threaded view
|

Re: Converting list with different vector lengths into a dataframe (or export it as .csv)

Olivereta
In reply to this post by arun kirshna
It worked, thank you very much indeed :)

Kind regards