Hi all, I have been experimenting with writing my own matrix column sum function. I want it to return a list. csum<-function(m) { a = data.frame(m) s = lapply(a,sum) return(s) } I wish to use the same code up until the return(s) that I have listed above. The problem is that s, I believe, is a data frame (looks like this:) $X1 [1] 148 $X2 [1] 156 $X3 [1] 164 $X4 [1] 172 $X5 [1] 180 I would like a vector with these values (148,156,etc). How may I do this? tia! -- View this message in context: http://www.nabble.com/dataframe-to-list-conversion-tp24682262p24682262.html Sent from the R help mailing list archive at Nabble.com.
have a look at ?unlist(); you can also use sapply() in this case instead of lapply(). Best, Dimitris voidobscura wrote:> Hi all, I have been experimenting with writing my own matrix column sum > function. I want it to return a list. > > csum<-function(m) > { > a = data.frame(m) > s = lapply(a,sum) > return(s) > } > > I wish to use the same code up until the return(s) that I have listed above. > The problem is that s, I believe, is a data frame (looks like this:) > > $X1 > [1] 148 > > $X2 > [1] 156 > > $X3 > [1] 164 > > $X4 > [1] 172 > > $X5 > [1] 180 > > I would like a vector with these values (148,156,etc). How may I do this? > tia!-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Hi voidobscura, Try either csum2 <- function(m){ a = data.frame(m) s = lapply(a,sum) do.call(c, s) } or colSums(m) See ?do.call and ?colSums for more details. HTH, Jorge On Mon, Jul 27, 2009 at 11:03 AM, voidobscura <nshah171@gmail.com> wrote:> > Hi all, I have been experimenting with writing my own matrix column sum > function. I want it to return a list. > > csum<-function(m) > { > a = data.frame(m) > s = lapply(a,sum) > return(s) > } > > I wish to use the same code up until the return(s) that I have listed > above. > The problem is that s, I believe, is a data frame (looks like this:) > > $X1 > [1] 148 > > $X2 > [1] 156 > > $X3 > [1] 164 > > $X4 > [1] 172 > > $X5 > [1] 180 > > I would like a vector with these values (148,156,etc). How may I do this? > tia! > -- > View this message in context: > http://www.nabble.com/dataframe-to-list-conversion-tp24682262p24682262.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
All you need is csum <- function(m) sapply(m, sum) (in which case making csum a function does not achieve very much). If for some reason you want to hang on to your code and just modify the last line (though I cannot think why, but still...) you could do csum <- function(m) { a <- data.frame(m) ## not needed s <- lapply(a,sum) unlist(s) ## simplifies the list into a vector. } Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of voidobscura Sent: Tuesday, 28 July 2009 1:04 AM To: r-help at r-project.org Subject: [R] dataframe to list conversion Hi all, I have been experimenting with writing my own matrix column sum function. I want it to return a list. csum<-function(m) { a = data.frame(m) s = lapply(a,sum) return(s) } I wish to use the same code up until the return(s) that I have listed above. The problem is that s, I believe, is a data frame (looks like this:) $X1 [1] 148 $X2 [1] 156 $X3 [1] 164 $X4 [1] 172 $X5 [1] 180 I would like a vector with these values (148,156,etc). How may I do this? tia! -- View this message in context: http://www.nabble.com/dataframe-to-list-conversion-tp24682262p24682262.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Dear voidobscura, Try also Csum <- function(x) apply(x, 2, sum) Csum(m) HTH, Jorge On Mon, Jul 27, 2009 at 11:03 AM, voidobscura <> wrote:> > Hi all, I have been experimenting with writing my own matrix column sum > function. I want it to return a list. > > csum<-function(m) > { > a = data.frame(m) > s = lapply(a,sum) > return(s) > } > > I wish to use the same code up until the return(s) that I have listed > above. > The problem is that s, I believe, is a data frame (looks like this:) > > $X1 > [1] 148 > > $X2 > [1] 156 > > $X3 > [1] 164 > > $X4 > [1] 172 > > $X5 > [1] 180 > > I would like a vector with these values (148,156,etc). How may I do this? > tia! > -- > View this message in context: > http://www.nabble.com/dataframe-to-list-conversion-tp24682262p24682262.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]