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










share|improve this question
























  • 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










  • 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















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










share|improve this question
























  • 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










  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 20:40









Henrik

40.5k991107




40.5k991107










asked Nov 19 at 18:06









SQLNewUser

103




103












  • 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










  • 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


















  • 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










  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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