Applying ifelse function to transform entire column in data frame
I have a data frame such as this:
df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))
And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).
The closest I've gotten is this:
df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)
Thanks!
r dataframe if-statement grepl
add a comment |
I have a data frame such as this:
df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))
And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).
The closest I've gotten is this:
df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)
Thanks!
r dataframe if-statement grepl
1
Your problem description doesn't match the posted expected output. In that output you multiply the entire columnyby-1, without considering the value of columnx.
– Rui Barradas
Nov 25 '18 at 17:46
add a comment |
I have a data frame such as this:
df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))
And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).
The closest I've gotten is this:
df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)
Thanks!
r dataframe if-statement grepl
I have a data frame such as this:
df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))
And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).
The closest I've gotten is this:
df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)
Thanks!
r dataframe if-statement grepl
r dataframe if-statement grepl
asked Nov 25 '18 at 17:32
Marco Pastor MayoMarco Pastor Mayo
767
767
1
Your problem description doesn't match the posted expected output. In that output you multiply the entire columnyby-1, without considering the value of columnx.
– Rui Barradas
Nov 25 '18 at 17:46
add a comment |
1
Your problem description doesn't match the posted expected output. In that output you multiply the entire columnyby-1, without considering the value of columnx.
– Rui Barradas
Nov 25 '18 at 17:46
1
1
Your problem description doesn't match the posted expected output. In that output you multiply the entire column
y by -1, without considering the value of column x.– Rui Barradas
Nov 25 '18 at 17:46
Your problem description doesn't match the posted expected output. In that output you multiply the entire column
y by -1, without considering the value of column x.– Rui Barradas
Nov 25 '18 at 17:46
add a comment |
1 Answer
1
active
oldest
votes
You could just do:
df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)
If you want to multiply all, then something like:
df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y
This only results in flipping the value ofdf$ywhendf$x="A", but I want to flip alldf$yvalues whendf$x="A".
– Marco Pastor Mayo
Nov 26 '18 at 15:57
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53470081%2fapplying-ifelse-function-to-transform-entire-column-in-data-frame%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could just do:
df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)
If you want to multiply all, then something like:
df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y
This only results in flipping the value ofdf$ywhendf$x="A", but I want to flip alldf$yvalues whendf$x="A".
– Marco Pastor Mayo
Nov 26 '18 at 15:57
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
add a comment |
You could just do:
df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)
If you want to multiply all, then something like:
df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y
This only results in flipping the value ofdf$ywhendf$x="A", but I want to flip alldf$yvalues whendf$x="A".
– Marco Pastor Mayo
Nov 26 '18 at 15:57
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
add a comment |
You could just do:
df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)
If you want to multiply all, then something like:
df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y
You could just do:
df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)
If you want to multiply all, then something like:
df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y
edited Nov 26 '18 at 16:18
answered Nov 25 '18 at 17:36
arg0nautarg0naut
5,7641421
5,7641421
This only results in flipping the value ofdf$ywhendf$x="A", but I want to flip alldf$yvalues whendf$x="A".
– Marco Pastor Mayo
Nov 26 '18 at 15:57
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
add a comment |
This only results in flipping the value ofdf$ywhendf$x="A", but I want to flip alldf$yvalues whendf$x="A".
– Marco Pastor Mayo
Nov 26 '18 at 15:57
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
This only results in flipping the value of
df$y when df$x="A", but I want to flip all df$y values when df$x="A".– Marco Pastor Mayo
Nov 26 '18 at 15:57
This only results in flipping the value of
df$y when df$x="A", but I want to flip all df$y values when df$x="A".– Marco Pastor Mayo
Nov 26 '18 at 15:57
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
In that case, see my edit.
– arg0naut
Nov 26 '18 at 16:18
add a comment |
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.
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%2f53470081%2fapplying-ifelse-function-to-transform-entire-column-in-data-frame%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
1
Your problem description doesn't match the posted expected output. In that output you multiply the entire column
yby-1, without considering the value of columnx.– Rui Barradas
Nov 25 '18 at 17:46