How to reference a variable which is not in data.table header?
up vote
0
down vote
favorite
I have a data.table DT, which contains 2 columns.
library(data.table)
DT = data.table(a = c(1, 2), b = c(2, 3))
I have a variable, named i
i <- "A"
Now I want to use i to determine how to create a new column in data.table. If i == "A", I want to create a new column, which is a*b.
However, if I use DT[ , NEW := ifelse(i == "A", a*b, a)]
The result is in below, which is not what I want.
a b NEW
1: 1 2 2
2: 2 3 2
The result that I am looking for is
a b NEW
1: 1 2 2
2: 2 3 6
I know if I generate a new column named i, I can get what I want.
DT[ , i := i]
DT[ , NEW := ifelse(i == "A", a*b, a)]
a b i NEW
1: 1 2 A 2
2: 2 3 A 6
Is there a simple way to reference variable i, without generating a new column named i?
Thanks
r data.table
|
show 1 more comment
up vote
0
down vote
favorite
I have a data.table DT, which contains 2 columns.
library(data.table)
DT = data.table(a = c(1, 2), b = c(2, 3))
I have a variable, named i
i <- "A"
Now I want to use i to determine how to create a new column in data.table. If i == "A", I want to create a new column, which is a*b.
However, if I use DT[ , NEW := ifelse(i == "A", a*b, a)]
The result is in below, which is not what I want.
a b NEW
1: 1 2 2
2: 2 3 2
The result that I am looking for is
a b NEW
1: 1 2 2
2: 2 3 6
I know if I generate a new column named i, I can get what I want.
DT[ , i := i]
DT[ , NEW := ifelse(i == "A", a*b, a)]
a b i NEW
1: 1 2 A 2
2: 2 3 A 6
Is there a simple way to reference variable i, without generating a new column named i?
Thanks
r data.table
useif/elsenotifelse
– MichaelChirico
Nov 19 at 18:20
stackoverflow.com/questions/17252905/else-if-vs-ifelse
– MichaelChirico
Nov 19 at 18:20
also may be a situation where you consider usingswitch; see?switch
– MichaelChirico
Nov 19 at 18:21
3
Or raise 'b' to the power of your logical test,DT[ , NEW := a * b^(i == "A")]
– Henrik
Nov 19 at 18:44
@MichaelChirico Thank you! Simple if/else works.
– SQLNewUser
Nov 19 at 21:00
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a data.table DT, which contains 2 columns.
library(data.table)
DT = data.table(a = c(1, 2), b = c(2, 3))
I have a variable, named i
i <- "A"
Now I want to use i to determine how to create a new column in data.table. If i == "A", I want to create a new column, which is a*b.
However, if I use DT[ , NEW := ifelse(i == "A", a*b, a)]
The result is in below, which is not what I want.
a b NEW
1: 1 2 2
2: 2 3 2
The result that I am looking for is
a b NEW
1: 1 2 2
2: 2 3 6
I know if I generate a new column named i, I can get what I want.
DT[ , i := i]
DT[ , NEW := ifelse(i == "A", a*b, a)]
a b i NEW
1: 1 2 A 2
2: 2 3 A 6
Is there a simple way to reference variable i, without generating a new column named i?
Thanks
r data.table
I have a data.table DT, which contains 2 columns.
library(data.table)
DT = data.table(a = c(1, 2), b = c(2, 3))
I have a variable, named i
i <- "A"
Now I want to use i to determine how to create a new column in data.table. If i == "A", I want to create a new column, which is a*b.
However, if I use DT[ , NEW := ifelse(i == "A", a*b, a)]
The result is in below, which is not what I want.
a b NEW
1: 1 2 2
2: 2 3 2
The result that I am looking for is
a b NEW
1: 1 2 2
2: 2 3 6
I know if I generate a new column named i, I can get what I want.
DT[ , i := i]
DT[ , NEW := ifelse(i == "A", a*b, a)]
a b i NEW
1: 1 2 A 2
2: 2 3 A 6
Is there a simple way to reference variable i, without generating a new column named i?
Thanks
r data.table
r data.table
edited Nov 20 at 20:40
Henrik
40.5k991107
40.5k991107
asked Nov 19 at 18:06
SQLNewUser
103
103
useif/elsenotifelse
– MichaelChirico
Nov 19 at 18:20
stackoverflow.com/questions/17252905/else-if-vs-ifelse
– MichaelChirico
Nov 19 at 18:20
also may be a situation where you consider usingswitch; see?switch
– MichaelChirico
Nov 19 at 18:21
3
Or raise 'b' to the power of your logical test,DT[ , NEW := a * b^(i == "A")]
– Henrik
Nov 19 at 18:44
@MichaelChirico Thank you! Simple if/else works.
– SQLNewUser
Nov 19 at 21:00
|
show 1 more comment
useif/elsenotifelse
– MichaelChirico
Nov 19 at 18:20
stackoverflow.com/questions/17252905/else-if-vs-ifelse
– MichaelChirico
Nov 19 at 18:20
also may be a situation where you consider usingswitch; see?switch
– MichaelChirico
Nov 19 at 18:21
3
Or raise 'b' to the power of your logical test,DT[ , NEW := a * b^(i == "A")]
– Henrik
Nov 19 at 18:44
@MichaelChirico Thank you! Simple if/else works.
– SQLNewUser
Nov 19 at 21:00
use
if/else not ifelse– MichaelChirico
Nov 19 at 18:20
use
if/else not ifelse– MichaelChirico
Nov 19 at 18:20
stackoverflow.com/questions/17252905/else-if-vs-ifelse
– MichaelChirico
Nov 19 at 18:20
stackoverflow.com/questions/17252905/else-if-vs-ifelse
– MichaelChirico
Nov 19 at 18:20
also may be a situation where you consider using
switch; see ?switch– MichaelChirico
Nov 19 at 18:21
also may be a situation where you consider using
switch; see ?switch– MichaelChirico
Nov 19 at 18:21
3
3
Or raise 'b' to the power of your logical test,
DT[ , NEW := a * b^(i == "A")]– Henrik
Nov 19 at 18:44
Or raise 'b' to the power of your logical test,
DT[ , NEW := a * b^(i == "A")]– Henrik
Nov 19 at 18:44
@MichaelChirico Thank you! Simple if/else works.
– SQLNewUser
Nov 19 at 21:00
@MichaelChirico Thank you! Simple if/else works.
– SQLNewUser
Nov 19 at 21:00
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380345%2fhow-to-reference-a-variable-which-is-not-in-data-table-header%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use
if/elsenotifelse– MichaelChirico
Nov 19 at 18:20
stackoverflow.com/questions/17252905/else-if-vs-ifelse
– MichaelChirico
Nov 19 at 18:20
also may be a situation where you consider using
switch; see?switch– MichaelChirico
Nov 19 at 18:21
3
Or raise 'b' to the power of your logical test,
DT[ , NEW := a * b^(i == "A")]– Henrik
Nov 19 at 18:44
@MichaelChirico Thank you! Simple if/else works.
– SQLNewUser
Nov 19 at 21:00