[R] converting lists got by tapply to dataframes
I have two lists: xa - list( X=c(1,2,3), Y=c(4,5,6), Z=c(7,8,9) ) xb - with( barley, tapply( X=seq(1:nrow(barley)), INDEX=site , FUN=function(z)yield[z])) I can convert xa to a dataframe easily with: as.data.frame(xa) But if i try the same with xb I get: as.data.frame(xb) Error in as.data.frame.default(xb) : can't coerce array into a data.frame What helps? (NB: I know the formula for xb is stupid, but it generates the same type of list as the list I get from my real problem.) Wolfram __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] converting lists got by tapply to dataframes
Wolfram, try unlist(xb) Andrew On Wednesday 10 March 2004 06:58, Wolfram Fischer wrote: I have two lists: xa - list( X=c(1,2,3), Y=c(4,5,6), Z=c(7,8,9) ) xb - with( barley, tapply( X=seq(1:nrow(barley)), INDEX=site , FUN=function(z)yield[z])) I can convert xa to a dataframe easily with: as.data.frame(xa) But if i try the same with xb I get: as.data.frame(xb) Error in as.data.frame.default(xb) : can't coerce array into a data.frame What helps? (NB: I know the formula for xb is stupid, but it generates the same type of list as the list I get from my real problem.) Wolfram __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Andrew Robinson Ph: 208 885 7115 Department of Forest Resources Fa: 208 885 6226 University of Idaho E : [EMAIL PROTECTED] PO Box 441133W : http://www.uidaho.edu/~andrewr Moscow ID 83843 Or: http://www.biometrics.uidaho.edu No statement above necessarily represents my employer's opinion. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] converting lists got by tapply to dataframes
That gives you a vector, not a data frame. The problem is that the object returned by tapply is a 1-d array of list. When as.data.frame() is called, the default method doesn't know how to handle arrays and gives the error. What you really want for this particular case is to treat the object as a list, so the unorthodox way is: as.data.frame.list(xb) OK, I didn't really say that... Andy From: Andrew Robinson Wolfram, try unlist(xb) Andrew On Wednesday 10 March 2004 06:58, Wolfram Fischer wrote: I have two lists: xa - list( X=c(1,2,3), Y=c(4,5,6), Z=c(7,8,9) ) xb - with( barley, tapply( X=seq(1:nrow(barley)), INDEX=site , FUN=function(z)yield[z])) I can convert xa to a dataframe easily with: as.data.frame(xa) But if i try the same with xb I get: as.data.frame(xb) Error in as.data.frame.default(xb) : can't coerce array into a data.frame What helps? (NB: I know the formula for xb is stupid, but it generates the same type of list as the list I get from my real problem.) Wolfram __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Andrew Robinson Ph: 208 885 7115 Department of Forest Resources Fa: 208 885 6226 University of Idaho E : [EMAIL PROTECTED] PO Box 441133W : http://www.uidaho.edu/~andrewr Moscow ID 83843 Or: http://www.biometrics.uidaho.edu No statement above necessarily represents my employer's opinion. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Notice: This e-mail message, together with any attachments,...{{dropped}} __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] converting lists got by tapply to dataframes
On Wed, 10 Mar 2004, Liaw, Andy wrote: That gives you a vector, not a data frame. The problem is that the object returned by tapply is a 1-d array of list. When as.data.frame() is called, the default method doesn't know how to handle arrays and gives the error. What you really want for this particular case is to treat the object as a list, so the unorthodox way is: as.data.frame.list(xb) OK, I didn't really say that... If this dataset barley from package lattice, it works in 1.9.0 alpha which does have as.data.frame.array. So the issue is about to go away. On Wednesday 10 March 2004 06:58, Wolfram Fischer wrote: I have two lists: xa - list( X=c(1,2,3), Y=c(4,5,6), Z=c(7,8,9) ) xb - with( barley, tapply( X=seq(1:nrow(barley)), INDEX=site , FUN=function(z)yield[z])) I can convert xa to a dataframe easily with: as.data.frame(xa) But if i try the same with xb I get: as.data.frame(xb) Error in as.data.frame.default(xb) : can't coerce array into a data.frame What helps? (NB: I know the formula for xb is stupid, but it generates the same type of list as the list I get from my real problem.) -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] converting lists got by tapply to dataframes
On 10 Mar 2004 at 15:58, Wolfram Fischer wrote: It's a bit strange. The following should work: as.data.frame(as.matrix(xb)) Kjetil Halvorsen I have two lists: xa - list( X=c(1,2,3), Y=c(4,5,6), Z=c(7,8,9) ) xb - with( barley, tapply( X=seq(1:nrow(barley)), INDEX=site , FUN=function(z)yield[z])) I can convert xa to a dataframe easily with: as.data.frame(xa) But if i try the same with xb I get: as.data.frame(xb) Error in as.data.frame.default(xb) : can't coerce array into a data.frame What helps? (NB: I know the formula for xb is stupid, but it generates the same type of list as the list I get from my real problem.) Wolfram __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html