|
Hi,
I am importing dataframe from an Excel file (xlsx package). The columns contain acutally measurements for single species and the column-length is of variable. As it is imported as a dataframe the difference to the "longest" column is filled with NA. To explain it with an example, my dataframe looks like: A <- seq(1:10) B <- c(seq(1:5),rep(NA,5)) C <- c(seq(1:7),rep(NA,3)) df <- data.frame(A,B,C) Now I'd like to transform that to a list of vectors of different length. Therefore I need to remove the NAs collectively from the single columns...I tried for transforming: as.list(df) ...but I don't know how can I remove the NAs now? as.list doesn't take na.rm=TRUE argument. Is there any ready function to perform such tasks? Or is there a better way then to assign the data to a list of vectors with variable length? /johannes -- ______________________________________________ [hidden email] 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. |
|
Two possibilities are:
lapply(df, function (x) x[!is.na(x)]) and lapply(df, na.exclude) I hope it helps. Best, Dimitris On 2/8/2012 11:54 AM, Johannes Radinger wrote: > Hi, > > I am importing dataframe from an Excel file (xlsx package). > The columns contain acutally measurements for single species and > the column-length is of variable. As it is imported as a dataframe the difference to the "longest" column is filled with NA. > To explain it with an example, my dataframe looks like: > > A<- seq(1:10) > B<- c(seq(1:5),rep(NA,5)) > C<- c(seq(1:7),rep(NA,3)) > > df<- data.frame(A,B,C) > > > Now I'd like to transform that to a list of vectors of different length. Therefore I need to remove the NAs collectively from the single columns...I tried for transforming: > > as.list(df) > > ...but I don't know how can I remove the NAs now? as.list doesn't take na.rm=TRUE argument. Is there any ready function to perform such tasks? > Or is there a better way then to assign the data to a list of vectors with variable length? > > /johannes > -- > > ______________________________________________ > [hidden email] 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. > -- 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 Web: http://www.erasmusmc.nl/biostatistiek/ ______________________________________________ [hidden email] 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. |
|
Hi,
lapply(df, function (x) x[!is.na(x)]) thats is really great! Thank you! -------- Original-Nachricht -------- > Datum: Wed, 08 Feb 2012 12:01:17 +0100 > Von: Dimitris Rizopoulos <[hidden email]> > An: Johannes Radinger <[hidden email]> > CC: [hidden email] > Betreff: Re: [R] remove NAs from list collectively > Two possibilities are: > > lapply(df, function (x) x[!is.na(x)]) > > and > > lapply(df, na.exclude) > > > I hope it helps. > > Best, > Dimitris > > > On 2/8/2012 11:54 AM, Johannes Radinger wrote: > > Hi, > > > > I am importing dataframe from an Excel file (xlsx package). > > The columns contain acutally measurements for single species and > > the column-length is of variable. As it is imported as a dataframe the > difference to the "longest" column is filled with NA. > > To explain it with an example, my dataframe looks like: > > > > A<- seq(1:10) > > B<- c(seq(1:5),rep(NA,5)) > > C<- c(seq(1:7),rep(NA,3)) > > > > df<- data.frame(A,B,C) > > > > > > Now I'd like to transform that to a list of vectors of different length. > Therefore I need to remove the NAs collectively from the single > columns...I tried for transforming: > > > > as.list(df) > > > > ...but I don't know how can I remove the NAs now? as.list doesn't take > na.rm=TRUE argument. Is there any ready function to perform such tasks? > > Or is there a better way then to assign the data to a list of vectors > with variable length? > > > > /johannes > > -- > > > > ______________________________________________ > > [hidden email] 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. > > > > -- > 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 > Web: http://www.erasmusmc.nl/biostatistiek/ -- ______________________________________________ [hidden email] 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. |
|
In reply to this post by D. Rizopoulos
Hi,
I want to "melt" my list and get certain deskriptive factors (length of a vector etc.) into a dataframe. Best to describe it with an example: A <- seq(4) B <- seq(6) C <- seq(9) ls <- list(A,B,C) # this is my list with vectors of different length # thats the dataframe how it should look like: name length(x) length(x[x>5]) length(x[x<5]) A 4 0 4 B 6 1 4 C 9 4 4 How can that be achieved? /johannes -- ______________________________________________ [hidden email] 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. |
|
Try
list <- list(1:4, 1:6, 1:9) t(sapply(list, function(x) c(length(x), sum(x > 5), sum(x < 5)))) HTH, Jorge.- On Wed, Feb 8, 2012 at 8:50 AM, Johannes Radinger <> wrote: > Hi, > I want to "melt" my list and get certain deskriptive factors (length of a > vector etc.) into a dataframe. Best to describe it with an example: > > A <- seq(4) > B <- seq(6) > C <- seq(9) > > ls <- list(A,B,C) # this is my list with vectors of different length > > # thats the dataframe how it should look like: > name length(x) length(x[x>5]) length(x[x<5]) > A 4 0 4 > B 6 1 4 > C 9 4 4 > > How can that be achieved? > > > /johannes > -- > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] 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. |
|
Hi,
> Try > > list <- list(1:4, 1:6, 1:9) > t(sapply(list, function(x) c(length(x), sum(x > 5), sum(x < 5)))) > thank you...the sapply approach seems straight forward, although I don't get the names into an own column... When the list elements are named the name is used for the rownames. I'd like to have them as an own column and no row names... like for the list: list <- list(A=1:4, B=1:6, C=1:9) t(sapply(list, function(x) c(length(x), sum(x > 5), sum(x < 5)))) /Johannes > HTH, > Jorge.- > > > On Wed, Feb 8, 2012 at 8:50 AM, Johannes Radinger <> wrote: > > > Hi, > > I want to "melt" my list and get certain deskriptive factors (length of > a > > vector etc.) into a dataframe. Best to describe it with an example: > > > > A <- seq(4) > > B <- seq(6) > > C <- seq(9) > > > > ls <- list(A,B,C) # this is my list with vectors of different length > > > > # thats the dataframe how it should look like: > > name length(x) length(x[x>5]) length(x[x<5]) > > A 4 0 4 > > B 6 1 4 > > C 9 4 4 > > > > How can that be achieved? > > > > > > /johannes > > -- > > > > ______________________________________________ > > [hidden email] 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. > > -- ______________________________________________ [hidden email] 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. |
|
Does this do what you want:
> list <- list(A=1:4, B=1:6, C=1:9) > result <- lapply(names(list), function(x){ + data.frame(name = x + , length = length(list[[x]]) + , gt5 = sum(list[[x]] > 5) + , lt5 = sum(list[[x]] < 5) + ) + }) > do.call(rbind, result) name length gt5 lt5 1 A 4 0 4 2 B 6 1 4 3 C 9 4 4 On Wed, Feb 8, 2012 at 10:18 AM, Johannes Radinger <[hidden email]> wrote: > Hi, > >> Try >> >> list <- list(1:4, 1:6, 1:9) >> t(sapply(list, function(x) c(length(x), sum(x > 5), sum(x < 5)))) >> > > thank you...the sapply approach seems straight forward, although I don't get the names into an own column... When the list elements are named the name is used for the rownames. I'd like to have them as an own column and no row names... > > like for the list: > list <- list(A=1:4, B=1:6, C=1:9) > t(sapply(list, function(x) c(length(x), sum(x > 5), sum(x < 5)))) > > /Johannes > >> HTH, >> Jorge.- >> >> >> On Wed, Feb 8, 2012 at 8:50 AM, Johannes Radinger <> wrote: >> >> > Hi, >> > I want to "melt" my list and get certain deskriptive factors (length of >> a >> > vector etc.) into a dataframe. Best to describe it with an example: >> > >> > A <- seq(4) >> > B <- seq(6) >> > C <- seq(9) >> > >> > ls <- list(A,B,C) # this is my list with vectors of different length >> > >> > # thats the dataframe how it should look like: >> > name length(x) length(x[x>5]) length(x[x<5]) >> > A 4 0 4 >> > B 6 1 4 >> > C 9 4 4 >> > >> > How can that be achieved? >> > >> > >> > /johannes >> > -- >> > >> > ______________________________________________ >> > [hidden email] 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. >> > > > -- > > ______________________________________________ > [hidden email] 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
