Dear all, assuming that I do have a dataframe like : x <- data.frame(TYPE=c("DEL", "DEL", "DUP", "TRA", "INV", "TRA"), CHRA=c("chr1", "chr1", "chr1", "chr1", "chr2", "chr2"), POSA=c(10, 15, 120, 340, 100, 220), CHRB=c("chr1", "chr1", "chr1", "chr2", "chr2", "chr1"), POSB=c(30, 100, 300, 20, 200, 320)) , how could I initiate another 2 columns in x, where each element in these 2 columns is going to be a list (the list could be updated later). Thank you ! Shall I do, for (i in 1:dim(x)[1]) { x$intersectA[i] <- list()} for (i in 1:dim(x)[1]) { x$intersectB[i] <- list()} nothing is happening. Thank you very much ! [[alternative HTML version deleted]]
Dear Bogdan, You are looking for x$intersectA <- vector("list", nrow(x)) Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.be Havenlaan 88 bus 73, 1000 Brussel www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// <https://www.inbo.be> 2018-07-25 8:55 GMT+02:00 Bogdan Tanasa <tanasa at gmail.com>:> Dear all, > > assuming that I do have a dataframe like : > > x <- data.frame(TYPE=c("DEL", "DEL", "DUP", "TRA", "INV", "TRA"), > CHRA=c("chr1", "chr1", "chr1", "chr1", "chr2", "chr2"), > POSA=c(10, 15, 120, 340, 100, 220), > CHRB=c("chr1", "chr1", "chr1", "chr2", "chr2", "chr1"), > POSB=c(30, 100, 300, 20, 200, 320)) , > > how could I initiate another 2 columns in x, where each element in these 2 > columns is going to be a list (the list could be updated later). Thank you > ! > > Shall I do, > > for (i in 1:dim(x)[1]) { x$intersectA[i] <- list()} > > for (i in 1:dim(x)[1]) { x$intersectB[i] <- list()} > > nothing is happening. Thank you very much ! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
Juan Telleria Ruiz de Aguirre
2018-Jul-25 07:56 UTC
[R] initiate elements in a dataframe with lists
Check tidyverse's purrr package: https://github.com/rstudio/cheatsheets/raw/master/purrr.pdf In the second page of the cheatsheet there is info on how to create list columns within a data.frame :) [[alternative HTML version deleted]]
Just for my understanding: Is a data.frame with list columns still a data.frame? Isn't it then a list? Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 25/07/2018 09:56, Juan Telleria Ruiz de Aguirre wrote:> Check tidyverse's purrr package: > > https://github.com/rstudio/cheatsheets/raw/master/purrr.pdf > > In the second page of the cheatsheet there is info on how to create list > columns within a data.frame :) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Thank you Juan. On Wed, Jul 25, 2018 at 12:56 AM, Juan Telleria Ruiz de Aguirre < jtelleria.rproject at gmail.com> wrote:> Check tidyverse's purrr package: > > https://github.com/rstudio/cheatsheets/raw/master/purrr.pdf > > In the second page of the cheatsheet there is info on how to create list > columns within a data.frame :) >[[alternative HTML version deleted]]
Dear Thierry and Juan, thank you for your help. Thank you all. Now, if I would like to add an element to the empty list, how shall I do : for example, shall i = 2, and j = 1, in a bit of more complex R code : x <- data.frame(TYPE=c("DEL", "DEL", "DUP", "TRA", "INV", "TRA"), CHRA=c("chr1", "chr1", "chr1", "chr1", "chr2", "chr2"), POSA=c(10, 15, 120, 340, 100, 220), CHRB=c("chr1", "chr1", "chr1", "chr2", "chr2", "chr1"), POSB=c(30, 100, 300, 20, 200, 320)) x$labA <- paste(x$CHRA, x$POSA, sep="_") x$labB <- paste(x$CHRB, x$POSB, sep="_") x$POSA_left <- x$POSA - 10 x$POSA_right <- x$POSA + 10 x$POSB_left <- x$POSB - 10 x$POSB_right <- x$POSB + 10 x$intersectA <- rep(list(list()), nrow(x)) x$intersectB <- rep(list(list()), nrow(x)) And we know that for i = 2, and j = 1, the condition is TRUE : i <- 2 j <- 1 if ( (x$CHRA[i] == x$CHRA[j] ) && (x$POSA[i] > x$POSA_left[j] ) && (x$POSA[i] < x$POSA_right[j] ) ){ x$intersectA[i] <- c(x$intersectA[i], x$labA[j])} the R code does not work. Thank you for your kind help ! On Wed, Jul 25, 2018 at 12:26 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:> Dear Bogdan, > > You are looking for x$intersectA <- vector("list", nrow(x)) > > Best regards, > > > ir. Thierry Onkelinx > Statisticus / Statistician > > Vlaamse Overheid / Government of Flanders > INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND > FOREST > Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance > thierry.onkelinx at inbo.be > Havenlaan 88 > <https://maps.google.com/?q=Havenlaan+88&entry=gmail&source=g> bus 73, > 1000 Brussel > www.inbo.be > > //////////////////////////////////////////////////////////// > /////////////////////////////// > To call in the statistician after the experiment is done may be no more > than asking him to perform a post-mortem examination: he may be able to say > what the experiment died of. ~ Sir Ronald Aylmer Fisher > The plural of anecdote is not data. ~ Roger Brinner > The combination of some data and an aching desire for an answer does not > ensure that a reasonable answer can be extracted from a given body of data. > ~ John Tukey > //////////////////////////////////////////////////////////// > /////////////////////////////// > > <https://www.inbo.be> > > 2018-07-25 8:55 GMT+02:00 Bogdan Tanasa <tanasa at gmail.com>: > >> Dear all, >> >> assuming that I do have a dataframe like : >> >> x <- data.frame(TYPE=c("DEL", "DEL", "DUP", "TRA", "INV", "TRA"), >> CHRA=c("chr1", "chr1", "chr1", "chr1", "chr2", "chr2"), >> POSA=c(10, 15, 120, 340, 100, 220), >> CHRB=c("chr1", "chr1", "chr1", "chr2", "chr2", "chr1"), >> POSB=c(30, 100, 300, 20, 200, 320)) , >> >> how could I initiate another 2 columns in x, where each element in these 2 >> columns is going to be a list (the list could be updated later). Thank >> you ! >> >> Shall I do, >> >> for (i in 1:dim(x)[1]) { x$intersectA[i] <- list()} >> >> for (i in 1:dim(x)[1]) { x$intersectB[i] <- list()} >> >> nothing is happening. Thank you very much ! >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]