[R] as.data.frame(do.call(rbind, lapply)) produces something weird

arun smartpink111 at yahoo.com
Fri Nov 9 21:56:24 CET 2012


HI,
If you don't want to list the column names explicitly,
you can try this:
 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<-z1[,-1]
 z2[]<-sapply(z2,function(x) as.numeric(as.character(x))) 
data.frame(a=z1[,1],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
 str(data.frame(a=z1[,1],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

A.K.

----- Original Message -----
From: Sam Steingold <sds at gnu.org>
To: r-help at r-project.org; arun <smartpink111 at yahoo.com>
Cc: 
Sent: Friday, November 9, 2012 3:00 PM
Subject: Re: as.data.frame(do.call(rbind, lapply)) produces something weird

> * arun <fznegcvax111 at lnubb.pbz> [2012-11-09 11:33:43 -0800]:
>
> 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))})

1. I don't want to have to list all the column names explicitly

2. I find the num->char->num conversion repugnant and unacceptable.

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://www.PetitionOnline.com/tap12009/
http://truepeace.org http://honestreporting.com http://ffii.org
What was the best thing before sliced bread?





More information about the R-help mailing list