data.table: reference column in "i"-part by column name in string object

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

data.table: reference column in "i"-part by column name in string object

Pascal.Niklaus
I am converting data.frame-based code to data.table, for performance
reasons.

Is there a way to refer to columns using expressions in the "i"-part?

Here is an example:

a <- data.table(x=rep(LETTERS[1:10],each=2), y=1:20)
v <- "x"

For the j-part, I can access the column whose name is stored in v as

a[,..v]

However, for the i-part I did not find a good way to achieve the same.
For a simple case, the following works

xx <- "B"
a[xx, on=v]

However, I can't see how this is easily expanded to more complex logical
expressions.

Pascal

______________________________________________
[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: data.table: reference column in "i"-part by column name in string object

Rui Barradas
Hello,

I don't understand the question.
Like this?

xx <- "B"
yy <- "D"
a[xx, on = v]
a[c(xx, yy), on = v]


Note that .(xx, yy) doesn't work. It outputs something else.

Could you give an example of more complicated expressions you have
doubts with?

Hope this helps,

Rui Barradas

Às 14:10 de 21/02/2019, Pascal A. Niklaus escreveu:

> I am converting data.frame-based code to data.table, for performance
> reasons.
>
> Is there a way to refer to columns using expressions in the "i"-part?
>
> Here is an example:
>
> a <- data.table(x=rep(LETTERS[1:10],each=2), y=1:20)
> v <- "x"
>
> For the j-part, I can access the column whose name is stored in v as
>
> a[,..v]
>
> However, for the i-part I did not find a good way to achieve the same.
> For a simple case, the following works
>
> xx <- "B"
> a[xx, on=v]
>
> However, I can't see how this is easily expanded to more complex logical
> expressions.
>
> Pascal
>
> ______________________________________________
> [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: data.table: reference column in "i"-part by column name in string object

Pascal.Niklaus
On 21.02.19 20:43, Rui Barradas wrote:

> Hello,
>
> I don't understand the question.
> Like this?
>
> xx <- "B"
> yy <- "D"
> a[xx, on = v]
> a[c(xx, yy), on = v]
>
> Note that .(xx, yy) doesn't work. It outputs something else.
>
> Could you give an example of more complicated expressions you have
> doubts with?

Let's assume I'd like to create a subset like this, in data.frame syntax:

d[ (d[[v]] == a | d$b == "c") & d$c>2, ]

Here I have a column referenced by its name (stored in v), and compared
to a value stored in a, and the whole thing is part of a more complex
logical expression. I could write it in this conventional way, but I
wondered whether there is a better way that would leverage some of
data.tables syntax.

Pascal

______________________________________________
[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.