initiate elements in a dataframe with lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

initiate elements in a dataframe with lists

Bogdan Tanasa
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Thierry Onkelinx
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
[hidden email]
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 <[hidden email]>:

> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Juan Telleria Ruiz de Aguirre
In reply to this post by Bogdan Tanasa
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Ivan Calandra-5
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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Juan Telleria Ruiz de Aguirre
> Just for my understanding:
> Is a data.frame with list columns still a data.frame? Isn't it then a list?

* A data.frame (or tibble) is a list of columns.
* In which each column must be from the same data type, in this case list().

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Juan Telleria Ruiz de Aguirre
By the way, this also works:

dfl <- data.frame(x = 1:3, y = I(list(1:2, 1:3, 1:4)))

As indicated in "Advanced R" book:
http://adv-r.had.co.nz/Data-structures.html#data-frames

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Ivan Calandra-5
In reply to this post by Juan Telleria Ruiz de Aguirre
At first I was actually thinking about this situation, which cannot work:|
data.frame(x = 1:3, y = list(1:2, 1:3, 1:4))

|But I had never thought about this:|
df$y <-list(1:2, 1:3, 1:4)|
And it actually makes sense. The final requirement here is that all
columns must have the same length!

I'm not sure though why one would need that. Why not use lists in that case?

Thanks!
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 11:56, Juan Telleria Ruiz de Aguirre wrote:
>> Just for my understanding:
>> Is a data.frame with list columns still a data.frame? Isn't it then a list?
> * A data.frame (or tibble) is a list of columns.
> * In which each column must be from the same data type, in this case list().
>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Dénes Tóth-2
In reply to this post by Ivan Calandra-5


On 07/25/2018 10:23 AM, Ivan Calandra wrote:
> Just for my understanding:
> Is a data.frame with list columns still a data.frame? Isn't it then a list?

A data.frame is a list of equally sized vectors - that is, each vector
must be of the same length. It is not required that the vector is an
atomic vector; it can be a list, too. By having equally sized vectors in
a list you can arrange the list in a two-dimensional matrix-like format,
append row names to them, and you get a data.frame.

Principally data.frame(x = 1:3, y = list(1:2, 1:3, 1:4)) should work,
but it doesn't, as it was recognized by others, too:
https://stackoverflow.com/questions/9547518/create-a-data-frame-where-a-column-is-a-list

Cheers,
Denes


>
> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Bogdan Tanasa
In reply to this post by Juan Telleria Ruiz de Aguirre
Thank you Juan.

On Wed, Jul 25, 2018 at 12:56 AM, Juan Telleria Ruiz de Aguirre <
[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Bogdan Tanasa
In reply to this post by Thierry Onkelinx
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 <[hidden email]
> 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
> [hidden email]
> 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 <[hidden email]>:
>
>> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Bogdan Tanasa
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 <
> [hidden email]> 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
>> [hidden email]
>> 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 <[hidden email]>:
>>
>>> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Huzefa Khalil
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 <[hidden email]> 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 <
>> [hidden email]> 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
>>> [hidden email]
>>> 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 <[hidden email]>:
>>>
>>>> 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]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Jeff Newmiller
In reply to this post by Bogdan Tanasa
The code below reeks of a misconception that lists are efficient to add
items to, which is a confusion with the computer science term "linked
list".  In R, a list is NOT a linked list... it is a vector, which means
the memory used by the list is allocated at the time it is created, and
REALLOCATED when a new item is added. The only reason you should use a
list is because you expect to put values of different types or shapes into
it, which does not appear to apply in this use case.

In R, you should make a valiant effort to create things right the first
time, and if that doesn't work then preallocate the space you will need in
the vectors you are working with. Since you have a need to store a
variable number of elements in each intersectX element, the column needs
to be a list but the elements of that list can perfectly well be character
vectors.

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)
                , stringsAsFactors = FALSE
                )
compareRng <- function( chr1, pos1, chr2, pos2, delta ) {
   ( chr1 == chr2
   & ( pos2 - delta ) < pos1
   & pos1 < ( pos2 + delta )
   )
}
makeIntersectX <- function( n, chrlabel, poslabel, delta ) {
   lgclidx <- rep( TRUE, nrow( x ) )
   lgclidx[ n ] <- FALSE
   x[[ chrlabel ]][ compareRng( x[[ chrlabel ]][ n ]
                     , x[[ poslabel ]][ n ]
                     , x[[ chrlabel ]]
                     , x[[ poslabel ]]
                     , delta
                     )
         & lgclidx
         ]
}

x$intersectA <- lapply( seq.int( nrow( x ) )
                       , makeIntersectX
                       , chrlabel = "CHRA"
                       , poslabel = "POSA"
                       , delta = 10L
                       )
x$intersectB <- lapply( seq.int( nrow( x ) )
                       , makeIntersectX
                       , chrlabel = "CHRB"
                       , poslabel = "POSB"
                       , delta = 21L
                       )
> x
   TYPE CHRA POSA CHRB POSB intersectA intersectB
1  DEL chr1   10 chr1   30       chr1
2  DEL chr1   15 chr1  100       chr1
3  DUP chr1  120 chr1  300                  chr1
4  TRA chr1  340 chr2   20
5  INV chr2  100 chr2  200
6  TRA chr2  220 chr1  320                  chr1

Note that depending on what you plan to do beyond this point, it might
actually be more performant to use a data frame with repeated rows instead
of list columns... but I cannot tell from what you have provided.

On Wed, 25 Jul 2018, Bogdan Tanasa wrote:

> 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 <[hidden email]
>> 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
>> [hidden email]
>> 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 <[hidden email]>:
>>
>>> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

Bogdan Tanasa
Dear Jeff, it is a precious help and a fabulous suggestion. I will slowly
go over the R code that you have sent. Thanks a lot !

On Wed, Jul 25, 2018 at 10:43 AM, Jeff Newmiller <[hidden email]>
wrote:

> The code below reeks of a misconception that lists are efficient to add
> items to, which is a confusion with the computer science term "linked
> list".  In R, a list is NOT a linked list... it is a vector, which means
> the memory used by the list is allocated at the time it is created, and
> REALLOCATED when a new item is added. The only reason you should use a list
> is because you expect to put values of different types or shapes into it,
> which does not appear to apply in this use case.
>
> In R, you should make a valiant effort to create things right the first
> time, and if that doesn't work then preallocate the space you will need in
> the vectors you are working with. Since you have a need to store a variable
> number of elements in each intersectX element, the column needs to be a
> list but the elements of that list can perfectly well be character vectors.
>
> 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)
>                , stringsAsFactors = FALSE
>                )
> compareRng <- function( chr1, pos1, chr2, pos2, delta ) {
>   ( chr1 == chr2
>   & ( pos2 - delta ) < pos1
>   & pos1 < ( pos2 + delta )
>   )
> }
> makeIntersectX <- function( n, chrlabel, poslabel, delta ) {
>   lgclidx <- rep( TRUE, nrow( x ) )
>   lgclidx[ n ] <- FALSE
>   x[[ chrlabel ]][ compareRng( x[[ chrlabel ]][ n ]
>                     , x[[ poslabel ]][ n ]
>                     , x[[ chrlabel ]]
>                     , x[[ poslabel ]]
>                     , delta
>                     )
>         & lgclidx
>         ]
> }
>
> x$intersectA <- lapply( seq.int( nrow( x ) )
>                       , makeIntersectX
>                       , chrlabel = "CHRA"
>                       , poslabel = "POSA"
>                       , delta = 10L
>                       )
> x$intersectB <- lapply( seq.int( nrow( x ) )
>                       , makeIntersectX
>                       , chrlabel = "CHRB"
>                       , poslabel = "POSB"
>                       , delta = 21L
>                       )
>
>> x
>>
>   TYPE CHRA POSA CHRB POSB intersectA intersectB
> 1  DEL chr1   10 chr1   30       chr1
> 2  DEL chr1   15 chr1  100       chr1
> 3  DUP chr1  120 chr1  300                  chr1
> 4  TRA chr1  340 chr2   20
> 5  INV chr2  100 chr2  200
> 6  TRA chr2  220 chr1  320                  chr1
>
> Note that depending on what you plan to do beyond this point, it might
> actually be more performant to use a data frame with repeated rows instead
> of list columns... but I cannot tell from what you have provided.
>
>
> On Wed, 25 Jul 2018, Bogdan Tanasa wrote:
>
> 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 <
>> [hidden email]
>>
>>> 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 &
>>> <https://maps.google.com/?q=Biometrie+%26+&entry=gmail&source=g>Kwaliteitszorg
>>> / Team Biometrics & Quality Assurance
>>> [hidden email]
>>> 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 <[hidden email]>:
>>>
>>> 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]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>>
> ------------------------------------------------------------
> ---------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live
> Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ------------------------------------------------------------
> ---------------
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: initiate elements in a dataframe with lists

R help mailing list-2
In reply to this post by Jeff Newmiller
If you need to make a list of long but unknown length you can save time by
adding the items to an environment, with names giving the order, then
converting the environment to a list when you are done filling the
environment.  E.g.,

> makeData
function (container, n)
{
    for (i in seq_len(n)) container[[sprintf("%06x", i)]] <- seq_len(i%%5)
    container
}
> # use an environment
> system.time(E <- makeData(new.env(parent=emptyenv()), 10^5))
   user  system elapsed
   0.38    0.00    0.38
> # convert environment to a list
> system.time(EL <- as.list(E, sorted=TRUE))
   user  system elapsed
   0.62    0.00    0.62
> # use a list
> system.time(L <- makeData(list(), 10^5))
   user  system elapsed
 142.56    1.46  153.78
> all.equal(EL, L)
[1] TRUE


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Jul 25, 2018 at 10:43 AM, Jeff Newmiller <[hidden email]>
wrote:

> The code below reeks of a misconception that lists are efficient to add
> items to, which is a confusion with the computer science term "linked
> list".  In R, a list is NOT a linked list... it is a vector, which means
> the memory used by the list is allocated at the time it is created, and
> REALLOCATED when a new item is added. The only reason you should use a list
> is because you expect to put values of different types or shapes into it,
> which does not appear to apply in this use case.
>
> In R, you should make a valiant effort to create things right the first
> time, and if that doesn't work then preallocate the space you will need in
> the vectors you are working with. Since you have a need to store a variable
> number of elements in each intersectX element, the column needs to be a
> list but the elements of that list can perfectly well be character vectors.
>
> 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)
>                , stringsAsFactors = FALSE
>                )
> compareRng <- function( chr1, pos1, chr2, pos2, delta ) {
>   ( chr1 == chr2
>   & ( pos2 - delta ) < pos1
>   & pos1 < ( pos2 + delta )
>   )
> }
> makeIntersectX <- function( n, chrlabel, poslabel, delta ) {
>   lgclidx <- rep( TRUE, nrow( x ) )
>   lgclidx[ n ] <- FALSE
>   x[[ chrlabel ]][ compareRng( x[[ chrlabel ]][ n ]
>                     , x[[ poslabel ]][ n ]
>                     , x[[ chrlabel ]]
>                     , x[[ poslabel ]]
>                     , delta
>                     )
>         & lgclidx
>         ]
> }
>
> x$intersectA <- lapply( seq.int( nrow( x ) )
>                       , makeIntersectX
>                       , chrlabel = "CHRA"
>                       , poslabel = "POSA"
>                       , delta = 10L
>                       )
> x$intersectB <- lapply( seq.int( nrow( x ) )
>                       , makeIntersectX
>                       , chrlabel = "CHRB"
>                       , poslabel = "POSB"
>                       , delta = 21L
>                       )
>
>> x
>>
>   TYPE CHRA POSA CHRB POSB intersectA intersectB
> 1  DEL chr1   10 chr1   30       chr1
> 2  DEL chr1   15 chr1  100       chr1
> 3  DUP chr1  120 chr1  300                  chr1
> 4  TRA chr1  340 chr2   20
> 5  INV chr2  100 chr2  200
> 6  TRA chr2  220 chr1  320                  chr1
>
> Note that depending on what you plan to do beyond this point, it might
> actually be more performant to use a data frame with repeated rows instead
> of list columns... but I cannot tell from what you have provided.
>
> On Wed, 25 Jul 2018, Bogdan Tanasa wrote:
>
> 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 <
>> [hidden email]
>>
>>> 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 &
>>> <https://maps.google.com/?q=Biometrie+%26+&entry=gmail&source=g>Kwaliteitszorg
>>> / Team Biometrics & Quality Assurance
>>> [hidden email]
>>> 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 <[hidden email]>:
>>>
>>> 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]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>>
> ------------------------------------------------------------
> ---------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live
> Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.