Recoding Values For a Single Column in R (includes NULL values)
I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.
data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))
data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0
data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1
car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")
data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)
r null rstudio
add a comment |
I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.
data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))
data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0
data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1
car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")
data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)
r null rstudio
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 22 '18 at 2:45
Doesdata.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1)
not work?
– RAB
Nov 22 '18 at 3:16
No, it recodes all values (including NULL) to 1.
– Kornel Krysa
Nov 22 '18 at 3:41
add a comment |
I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.
data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))
data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0
data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1
car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")
data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)
r null rstudio
I have a dataframe named data.comp and within it a column named RELAFFIL. The values within the column range from 22 to 107, but there are also numerous NULL values. I wanted to recode the values to 0 for all NULL values, and 1 for all other cases. I have tried multiple approaches and none have worked, FYI I list the ones I've tried below. Also FYI I have the following packages loaded: dplyr, readr, and car.
data.comp$RELAFFIL <- with(data.comp, ifelse(is.null(data.comp$RELAFFIL), 0, 1))
data.comp$RELAFFIL[is.null(data.comp$RELAFFIL)] <- 0
data.comp$RELAFFIL[is.finite(data.comp$RELAFFIL)] <- 1
car::recode(data.comp$RELAFFIL, "NULL = 0; else = 1")
data.comp$RELAFFIL <- data.comp$RELAFFIL %>% base::ifelse(is.null(data.comp$RELAFFIL), 0, 1)
r null rstudio
r null rstudio
asked Nov 22 '18 at 2:40
Kornel KrysaKornel Krysa
1
1
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 22 '18 at 2:45
Doesdata.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1)
not work?
– RAB
Nov 22 '18 at 3:16
No, it recodes all values (including NULL) to 1.
– Kornel Krysa
Nov 22 '18 at 3:41
add a comment |
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 22 '18 at 2:45
Doesdata.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1)
not work?
– RAB
Nov 22 '18 at 3:16
No, it recodes all values (including NULL) to 1.
– Kornel Krysa
Nov 22 '18 at 3:41
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 22 '18 at 2:45
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 22 '18 at 2:45
Does
data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1)
not work?– RAB
Nov 22 '18 at 3:16
Does
data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1)
not work?– RAB
Nov 22 '18 at 3:16
No, it recodes all values (including NULL) to 1.
– Kornel Krysa
Nov 22 '18 at 3:41
No, it recodes all values (including NULL) to 1.
– Kornel Krysa
Nov 22 '18 at 3:41
add a comment |
1 Answer
1
active
oldest
votes
Base R solution for this is to improt file mentioning NA strings as null. code is provided below
data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
data.comp[is.na(data.comp)] <- 0
data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)
this will give your required output
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%2f53423139%2frecoding-values-for-a-single-column-in-r-includes-null-values%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
Base R solution for this is to improt file mentioning NA strings as null. code is provided below
data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
data.comp[is.na(data.comp)] <- 0
data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)
this will give your required output
add a comment |
Base R solution for this is to improt file mentioning NA strings as null. code is provided below
data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
data.comp[is.na(data.comp)] <- 0
data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)
this will give your required output
add a comment |
Base R solution for this is to improt file mentioning NA strings as null. code is provided below
data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
data.comp[is.na(data.comp)] <- 0
data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)
this will give your required output
Base R solution for this is to improt file mentioning NA strings as null. code is provided below
data.comp <- read.csv("your csv file",stringsAsFactors = F,na.strings = c("null"))
data.comp[is.na(data.comp)] <- 0
data.comp$RELAFFI_new <-ifelse(data.comp$RELAFFIL == 0,0,1)
this will give your required output
answered Nov 22 '18 at 4:08
HunaidkhanHunaidkhan
979416
979416
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%2f53423139%2frecoding-values-for-a-single-column-in-r-includes-null-values%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
Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.
– Ronak Shah
Nov 22 '18 at 2:45
Does
data.comp$RELAFFIL <- ifelse(is.null(data.comp$RELAFFIL), 0, 1)
not work?– RAB
Nov 22 '18 at 3:16
No, it recodes all values (including NULL) to 1.
– Kornel Krysa
Nov 22 '18 at 3:41