How to recode multiple-conditionals to -1/0/+1?
I have my data like:
Name A B C
apple 1 -1 0
banana 2 -2 1
pear -3 0 1
I want to replace all positive value to -1, all negative values to +1, but remain the 0 to be 0, how do I achieve that?
r recode
add a comment |
I have my data like:
Name A B C
apple 1 -1 0
banana 2 -2 1
pear -3 0 1
I want to replace all positive value to -1, all negative values to +1, but remain the 0 to be 0, how do I achieve that?
r recode
add a comment |
I have my data like:
Name A B C
apple 1 -1 0
banana 2 -2 1
pear -3 0 1
I want to replace all positive value to -1, all negative values to +1, but remain the 0 to be 0, how do I achieve that?
r recode
I have my data like:
Name A B C
apple 1 -1 0
banana 2 -2 1
pear -3 0 1
I want to replace all positive value to -1, all negative values to +1, but remain the 0 to be 0, how do I achieve that?
r recode
r recode
edited Nov 21 '18 at 8:40
smci
14.9k674104
14.9k674104
asked Nov 21 '18 at 8:32
Lennon Lee Lennon Lee
498
498
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
We could use sign
to get the sign of all the elements of the dataframe and then reverse it by multiplying it to -1.
df[-1] <- sign(df[-1]) * -1
df
# Name A B C
#1 apple -1 1 0
#2 banana -1 1 -1
#3 pear 1 0 -1
From ?sign
sign returns a vector with the signs of the corresponding elements of x (the sign of a real number is 1, 0, or -1 if the number is positive, zero, or negative, respectively).
data
df <- structure(list(Name = structure(1:3, .Label = c("apple", "banana",
"pear"), class = "factor"), A = c(-1, -1, 1), B = c(1, 1, 0),
C = c(0, -1, -1)), .Names = c("Name", "A", "B", "C"), row.names = c(NA,
-3L), class = "data.frame")
1
why not simplify to-sign(df[-1])
?
– Andre Elrico
Nov 21 '18 at 9:11
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
add a comment |
By no means this is as good as the top voted, but here is an alternative:
ans <- df1[,-1] / -abs(df1[,-1])
ans[sapply(ans,is.nan)] <- 0
#> ans
# A B C
#1 -1 1 0
#2 -1 1 -1
#3 1 0 -1
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%2f53407990%2fhow-to-recode-multiple-conditionals-to-1-0-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
We could use sign
to get the sign of all the elements of the dataframe and then reverse it by multiplying it to -1.
df[-1] <- sign(df[-1]) * -1
df
# Name A B C
#1 apple -1 1 0
#2 banana -1 1 -1
#3 pear 1 0 -1
From ?sign
sign returns a vector with the signs of the corresponding elements of x (the sign of a real number is 1, 0, or -1 if the number is positive, zero, or negative, respectively).
data
df <- structure(list(Name = structure(1:3, .Label = c("apple", "banana",
"pear"), class = "factor"), A = c(-1, -1, 1), B = c(1, 1, 0),
C = c(0, -1, -1)), .Names = c("Name", "A", "B", "C"), row.names = c(NA,
-3L), class = "data.frame")
1
why not simplify to-sign(df[-1])
?
– Andre Elrico
Nov 21 '18 at 9:11
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
add a comment |
We could use sign
to get the sign of all the elements of the dataframe and then reverse it by multiplying it to -1.
df[-1] <- sign(df[-1]) * -1
df
# Name A B C
#1 apple -1 1 0
#2 banana -1 1 -1
#3 pear 1 0 -1
From ?sign
sign returns a vector with the signs of the corresponding elements of x (the sign of a real number is 1, 0, or -1 if the number is positive, zero, or negative, respectively).
data
df <- structure(list(Name = structure(1:3, .Label = c("apple", "banana",
"pear"), class = "factor"), A = c(-1, -1, 1), B = c(1, 1, 0),
C = c(0, -1, -1)), .Names = c("Name", "A", "B", "C"), row.names = c(NA,
-3L), class = "data.frame")
1
why not simplify to-sign(df[-1])
?
– Andre Elrico
Nov 21 '18 at 9:11
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
add a comment |
We could use sign
to get the sign of all the elements of the dataframe and then reverse it by multiplying it to -1.
df[-1] <- sign(df[-1]) * -1
df
# Name A B C
#1 apple -1 1 0
#2 banana -1 1 -1
#3 pear 1 0 -1
From ?sign
sign returns a vector with the signs of the corresponding elements of x (the sign of a real number is 1, 0, or -1 if the number is positive, zero, or negative, respectively).
data
df <- structure(list(Name = structure(1:3, .Label = c("apple", "banana",
"pear"), class = "factor"), A = c(-1, -1, 1), B = c(1, 1, 0),
C = c(0, -1, -1)), .Names = c("Name", "A", "B", "C"), row.names = c(NA,
-3L), class = "data.frame")
We could use sign
to get the sign of all the elements of the dataframe and then reverse it by multiplying it to -1.
df[-1] <- sign(df[-1]) * -1
df
# Name A B C
#1 apple -1 1 0
#2 banana -1 1 -1
#3 pear 1 0 -1
From ?sign
sign returns a vector with the signs of the corresponding elements of x (the sign of a real number is 1, 0, or -1 if the number is positive, zero, or negative, respectively).
data
df <- structure(list(Name = structure(1:3, .Label = c("apple", "banana",
"pear"), class = "factor"), A = c(-1, -1, 1), B = c(1, 1, 0),
C = c(0, -1, -1)), .Names = c("Name", "A", "B", "C"), row.names = c(NA,
-3L), class = "data.frame")
answered Nov 21 '18 at 8:36
Ronak ShahRonak Shah
38.2k104161
38.2k104161
1
why not simplify to-sign(df[-1])
?
– Andre Elrico
Nov 21 '18 at 9:11
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
add a comment |
1
why not simplify to-sign(df[-1])
?
– Andre Elrico
Nov 21 '18 at 9:11
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
1
1
why not simplify to
-sign(df[-1])
?– Andre Elrico
Nov 21 '18 at 9:11
why not simplify to
-sign(df[-1])
?– Andre Elrico
Nov 21 '18 at 9:11
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
@AndreElrico yes, that would give the same result as well.
– Ronak Shah
Nov 21 '18 at 9:16
add a comment |
By no means this is as good as the top voted, but here is an alternative:
ans <- df1[,-1] / -abs(df1[,-1])
ans[sapply(ans,is.nan)] <- 0
#> ans
# A B C
#1 -1 1 0
#2 -1 1 -1
#3 1 0 -1
add a comment |
By no means this is as good as the top voted, but here is an alternative:
ans <- df1[,-1] / -abs(df1[,-1])
ans[sapply(ans,is.nan)] <- 0
#> ans
# A B C
#1 -1 1 0
#2 -1 1 -1
#3 1 0 -1
add a comment |
By no means this is as good as the top voted, but here is an alternative:
ans <- df1[,-1] / -abs(df1[,-1])
ans[sapply(ans,is.nan)] <- 0
#> ans
# A B C
#1 -1 1 0
#2 -1 1 -1
#3 1 0 -1
By no means this is as good as the top voted, but here is an alternative:
ans <- df1[,-1] / -abs(df1[,-1])
ans[sapply(ans,is.nan)] <- 0
#> ans
# A B C
#1 -1 1 0
#2 -1 1 -1
#3 1 0 -1
answered Nov 21 '18 at 9:08
Andre ElricoAndre Elrico
5,72011029
5,72011029
add a comment |
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%2f53407990%2fhow-to-recode-multiple-conditionals-to-1-0-1%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