I have a list that gives me the number of occurrences of numbers 1, 2, 3 and 4. Sometimes a single in a casa just appears 0 and 1, in others only 2, and every combination you can think of. Eg> Caso [1:2]$ `9` 0 1 2 10 $ `13` 0 2 2 4 Can I turn it into a matrix consisting of 4 columns in which I put the number of occurrences of the previous numbers, and for cases where it does not appear any of those values put 0? Regards, Sebastian. [[alternative HTML version deleted]]
On Thu, Sep 26, 2013 at 2:02 PM, Sebastian Kruk <residuo.solow at gmail.com> wrote:> I have a list that gives me the number of occurrences of numbers 1, 2, 3 and > 4. > > > Sometimes a single in a casa just appears 0 and 1, in others only 2, and every > combination you can think of. > > > Eg > > >> Caso [1:2] > $ `9` > > > 0 1 > 2 10 > > > $ `13` > > > 0 2 > 2 4 > > > Can I turn it into a matrix consisting of 4 columns in which I put the > number of occurrences of the previous numbers, and for cases where it does > not appear any of those values put 0?When creating Caso make the components factors with levels 0:4. dat <- list("9" = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), "13" = c(0, 0, 4, 4, 4, 4)) Caso <- lapply(dat, factor, levels = 0:4) do.call(rbind, lapply(Caso, table)) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hi,
May be this help:
Please dput() the example dataset:
Caso<- structure(list(`9` = structure(c(2, 10), .Names = c("0",
"1")),
??? `13` = structure(c(2, 4), .Names = c("0", "2"))), .Names
= c("9",
"13"))
nm<-unique(unlist(lapply(Caso,names)))
?vec1<- numeric(length(nm))
names(vec1)<- nm
?do.call(rbind,lapply(Caso,function(x){ indx<- names(vec1)%in% names(x);
vec1[indx]<-x;vec1 }))
?#? 0? 1 2
#9? 2 10 0
#13 2? 0 4
A.K.
----- Original Message -----
From: Sebastian Kruk <residuo.solow at gmail.com>
To: R-help <r-help at r-project.org>
Cc:
Sent: Thursday, September 26, 2013 2:02 PM
Subject: [R] Help with list
I have a list that gives me the number of occurrences of numbers 1, 2, 3 and
4.
Sometimes a single in a casa just appears 0 and 1, in others only 2, and every
combination you can think of.
Eg
> Caso [1:2]
$ `9`
0 1
2 10
$ `13`
0 2
2 4
Can I turn it into a matrix consisting of 4 columns in which I put the
number of occurrences of the previous numbers, and for cases where it does
not appear any of those values put 0?
Regards,
Sebastian.
??? [[alternative HTML version deleted]]
______________________________________________
R-help at 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.