Mohammad Tanvir Ahamed
2015-Apr-06 06:05 UTC
[R] Combine list element by column name to make a dataframe
Hi ,?
I have a example list like follow?
############################################
lst<-list(setNames(c(1,10,50,60,70,80),c("id","id1","math","phy","che","bio")),setNames(c(2,20,45),c("id","id1","phy")),setNames(c(3,30,75),c("id","id1","bio")))
My expected outcome :?
---------------------------------------------------------------------
df<-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75))
colnames(df)<-c("id","id1","math","phy","che","bio")
row.names(df) <- NULL
df
############################################
Any suggestion will be appreciated .?
Thanks in advance.
?
Best regards
...........................?
Tanvir Ahamed
G?teborg, Sweden
[[alternative HTML version deleted]]
peter dalgaard
2015-Apr-06 08:41 UTC
[R] Combine list element by column name to make a dataframe
> On 06 Apr 2015, at 08:05 , Mohammad Tanvir Ahamed via R-help <r-help at r-project.org> wrote: > > Hi ,? > > I have a example list like follow? > > > ############################################ > > lst<-list(setNames(c(1,10,50,60,70,80),c("id","id1","math","phy","che","bio")),setNames(c(2,20,45),c("id","id1","phy")),setNames(c(3,30,75),c("id","id1","bio"))) > > > My expected outcome :? > > --------------------------------------------------------------------- > > df<-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75)) > > colnames(df)<-c("id","id1","math","phy","che","bio") > > row.names(df) <- NULL > > df > > ############################################ > > > Any suggestion will be appreciated .?Hmm, in principle this looks like a merge() problem, if you first convert each list element to a data frame. That could be painful to get right though. You could try something like this:> nm <- Reduce(union, lapply(lst, names)) # or just type it in > nm[1] "id" "id1" "math" "phy" "che" "bio"> blank <- setNames(rep(NA_real_, length(nm)), nm) > fill1 <- function(x, blank) {blank[names(x)] <- x; blank} > lapply(lst, fill1, blank)[[1]] id id1 math phy che bio 1 10 50 60 70 80 [[2]] id id1 math phy che bio 2 20 NA 45 NA NA [[3]] id id1 math phy che bio 3 30 NA NA NA 75> do.call(rbind,lapply(lst, fill1, blank))id id1 math phy che bio [1,] 1 10 50 60 70 80 [2,] 2 20 NA 45 NA NA [3,] 3 30 NA NA NA 75 (NB, this is a matrix, not a data frame, but so is your "df"!) Notice that this does not do a proper merge on the id fields, i.e. if you have two different records with different grades (say one with "che" and another with "phy") on the same id, you get two records, not one. However, that might well be what you wanted. (It is tempting to use> fill1 <- function(x) `[<-`(blank, names(x), x) > lapply(lst, fill1)which does seem to work, but should probably be avoided because of the risk of destructive modification.)> > Thanks in advance. > > ? > > Best regards > > > ...........................? > > Tanvir Ahamed > > G?teborg, Sweden > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Michael Hannon
2015-Apr-06 08:43 UTC
[R] Combine list element by column name to make a dataframe
Maybe something like the appended?
-- Mike
lst <- list(setNames(c(1,10,50,60,70,80),
c("id","id1","math","phy","che","bio")),
setNames(c(2,20,45),
c("id","id1","phy")),
setNames(c(3,30,75),
c("id","id1","bio")))
lst
df <- rbind(c(1,10,50,60,70,80),
c(2,20,NA,45,NA,NA),
c(3,30,NA,NA,NA,75))
colnames(df)<-c("id","id1","math","phy","che","bio")
row.names(df) <- NULL
df
allNames <- unique(unlist(lapply(lst, names)))
allNames
newLst <- lapply(lst, function(element) {
element[allNames]
})
newLst
df2 <- do.call(rbind, newLst)
df2
all.equal(df, df2)
On Sun, Apr 5, 2015 at 11:05 PM, Mohammad Tanvir Ahamed via R-help
<r-help at r-project.org> wrote:> Hi ,
>
> I have a example list like follow
>
>
> ############################################
>
>
lst<-list(setNames(c(1,10,50,60,70,80),c("id","id1","math","phy","che","bio")),setNames(c(2,20,45),c("id","id1","phy")),setNames(c(3,30,75),c("id","id1","bio")))
>
>
> My expected outcome :
>
> ---------------------------------------------------------------------
>
> df<-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75))
>
>
colnames(df)<-c("id","id1","math","phy","che","bio")
>
> row.names(df) <- NULL
>
> df
>
> ############################################
>
>
> Any suggestion will be appreciated .
>
> Thanks in advance.
>
>
>
> Best regards
>
>
> ...........................
>
> Tanvir Ahamed
>
> G?teborg, Sweden
>
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
Duncan Mackay
2015-Apr-07 03:33 UTC
[R] Combine list element by column name to make a dataframe
forgot to cc to list
have a look at https://stat.ethz.ch/pipermail/r-help/2012-January/300275.html
and other messages in the sequence
if you use Marc Schwartz's list2df you with have to transpose it with t()
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Mohammad
Tanvir Ahamed via R-help
Sent: Monday, 6 April 2015 16:06
To: r-help at r-project.org
Subject: [R] Combine list element by column name to make a dataframe
Hi ,?
I have a example list like follow?
############################################
lst<-list(setNames(c(1,10,50,60,70,80),c("id","id1","math","phy","che","bio")),setNames(c(2,20,45),c("id","id1","phy")),setNames(c(3,30,75),c("id","id1","bio")))
My expected outcome :?
---------------------------------------------------------------------
df<-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75))
colnames(df)<-c("id","id1","math","phy","che","bio")
row.names(df) <- NULL
df
############################################
Any suggestion will be appreciated .?
Thanks in advance.
?
Best regards
...........................?
Tanvir Ahamed
G?teborg, Sweden
[[alternative HTML version deleted]]