Skip to content
Back to formatted view

Raw Message

Message-ID: <1352489623.12749.YahooMailNeo@web142604.mail.bf1.yahoo.com>
Date: 2012-11-09T19:33:43Z
From: arun
Subject: as.data.frame(do.call(rbind, lapply)) produces something weird
In-Reply-To: <87mwyqd1ks.fsf@gnu.org>

Hi,
May be this helps:
z1<-as.data.frame(do.call(rbind,lapply(1:3,function(x) c(a=paste("a",x,sep=""),unlist(do.call(c,list(b=myfun(x),c=myfun(x*x*x))))))))
z2<-within(z1,{b.x<-as.numeric(as.character(b.x));b.y<-as.numeric(as.character(b.y));c.x<-as.numeric(as.character(c.x));c.y<-as.numeric(as.character(c.y))})
?str(z2)
#'data.frame':??? 3 obs. of? 5 variables:
# $ a? : Factor w/ 3 levels "a1","a2","a3": 1 2 3
# $ b.x: num? 1 2 3
# $ b.y: num? 1 4 9
# $ c.x: num? 1 8 27
# $ c.y: num? 1 64 729


?z2
#?? a b.x b.y c.x c.y
#1 a1?? 1?? 1?? 1?? 1
#2 a2?? 2?? 4?? 8? 64
#3 a3?? 3?? 9? 27 729
A.K.



----- Original Message -----
From: Sam Steingold <sds at gnu.org>
To: r-help at r-project.org
Cc: 
Sent: Friday, November 9, 2012 2:21 PM
Subject: [R] as.data.frame(do.call(rbind,lapply)) produces something weird

The following code:
--8<---------------cut here---------------start------------->8---
> myfun <- function (x) list(x=x,y=x*x)
> z <- as.data.frame(do.call(rbind,lapply(1:3,function(x) c(a=paste("a",x,sep=""),as.list(unlist(list(b=myfun(x),c=myfun(x*x*x))))))))
> z
?  a b.x b.y c.x c.y
1 a1?  1?  1?  1?  1
2 a2?  2?  4?  8? 64
3 a3?  3?  9? 27 729
--8<---------------cut here---------------end--------------->8---
the appearance of z is good, but str() and summary betray some weirdness:
--8<---------------cut here---------------start------------->8---
> str(z)
'data.frame':??? 3 obs. of? 5 variables:
$ a? :List of 3
? ..$ : chr "a1"
? ..$ : chr "a2"
? ..$ : chr "a3"
$ b.x:List of 3
? ..$ : int 1
? ..$ : int 2
? ..$ : int 3
$ b.y:List of 3
? ..$ : int 1
? ..$ : int 4
? ..$ : int 9
$ c.x:List of 3
? ..$ : int 1
? ..$ : int 8
? ..$ : int 27
$ c.y:List of 3
? ..$ : int 1
? ..$ : int 64
? ..$ : int 729
--8<---------------cut here---------------end--------------->8---
how do I ensure that the columns of z are vectors, as in
--8<---------------cut here---------------start------------->8---
> z <- data.frame(a=c("a1","a2","a3"),b.x=c(1,2,3),b.y=c(1,4,9),c.x=c(1,8,27),c.y=c(1,64,729))
> z
?  a b.x b.y c.x c.y
1 a1?  1?  1?  1?  1
2 a2?  2?  4?  8? 64
3 a3?  3?  9? 27 729
> str(z)
'data.frame':??? 3 obs. of? 5 variables:
$ a? : Factor w/ 3 levels "a1","a2","a3": 1 2 3
$ b.x: num? 1 2 3
$ b.y: num? 1 4 9
$ c.x: num? 1 8 27
$ c.y: num? 1 64 729
--8<---------------cut here---------------end--------------->8---
thanks!

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://jihadwatch.org http://think-israel.org
http://www.PetitionOnline.com/tap12009/ http://honestreporting.com
Programming is like sex: one mistake and you have to support it for a lifetime.

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