[R] initiate elements in a dataframe with lists

Huzefa Khalil huze|@@kh@||| @end|ng |rom um|ch@edu
Wed Jul 25 15:40:19 CEST 2018


Hi Bogdan,

Does the following do what you expect?

x$intersectA[[i]] <- c(x$intersectA[[i]], x$labA[j])

Note the difference between `[[` and `[`


On Wed, Jul 25, 2018 at 9:26 AM, Bogdan Tanasa <tanasa using gmail.com> wrote:
> Dear Thierry and Juan, thank you for your help. Thank you very much.
>
> 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 using 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 using 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 using 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 using 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]]
>
> ______________________________________________
> R-help using 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.




More information about the R-help mailing list