how to I replace numeric values with a string in an R dataframe?
$begingroup$
I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.
df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')
Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.
Also tried
df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')
Any pointers highly appreciated.
r dataframe
$endgroup$
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.
df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')
Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.
Also tried
df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')
Any pointers highly appreciated.
r dataframe
$endgroup$
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40
$begingroup$
df[is.numeric(df)]="string".
$endgroup$
– user2974951
Sep 20 '18 at 6:31
add a comment |
$begingroup$
I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.
df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')
Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.
Also tried
df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')
Any pointers highly appreciated.
r dataframe
$endgroup$
I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.
df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')
Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.
Also tried
df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')
Any pointers highly appreciated.
r dataframe
r dataframe
asked Sep 19 '18 at 16:31
ramsrams
1011
1011
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 hour ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40
$begingroup$
df[is.numeric(df)]="string".
$endgroup$
– user2974951
Sep 20 '18 at 6:31
add a comment |
1
$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40
$begingroup$
df[is.numeric(df)]="string".
$endgroup$
– user2974951
Sep 20 '18 at 6:31
1
1
$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40
$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40
$begingroup$
df[is.numeric(df)]="string".$endgroup$
– user2974951
Sep 20 '18 at 6:31
$begingroup$
df[is.numeric(df)]="string".$endgroup$
– user2974951
Sep 20 '18 at 6:31
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
From the documentation of is.numeric:
The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.
So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.
is.numeric(c(5, 4, 3))
[1] TRUE
is.numeric(c(5, 4, NA))
[1] TRUE
That's why either all or none of the values are changed to NOTMISSING.
@eg-r's fix is correct. Here's a tidyverse way to accomplish the same.
> df<-tibble(myCol=c(5, 4, NA))
> df
# A tibble: 3 x 1
myCol
<dbl>
1 5
2 4
3 NA
> df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
# A tibble: 3 x 1
myCol
<chr>
1 NOTMISSING
2 NOTMISSING
3 <NA>
$endgroup$
add a comment |
$begingroup$
NAs can be numeric (especially if the other values in that column are all numeric). Try this :
df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)
Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :
df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f38497%2fhow-to-i-replace-numeric-values-with-a-string-in-an-r-dataframe%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
$begingroup$
From the documentation of is.numeric:
The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.
So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.
is.numeric(c(5, 4, 3))
[1] TRUE
is.numeric(c(5, 4, NA))
[1] TRUE
That's why either all or none of the values are changed to NOTMISSING.
@eg-r's fix is correct. Here's a tidyverse way to accomplish the same.
> df<-tibble(myCol=c(5, 4, NA))
> df
# A tibble: 3 x 1
myCol
<dbl>
1 5
2 4
3 NA
> df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
# A tibble: 3 x 1
myCol
<chr>
1 NOTMISSING
2 NOTMISSING
3 <NA>
$endgroup$
add a comment |
$begingroup$
From the documentation of is.numeric:
The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.
So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.
is.numeric(c(5, 4, 3))
[1] TRUE
is.numeric(c(5, 4, NA))
[1] TRUE
That's why either all or none of the values are changed to NOTMISSING.
@eg-r's fix is correct. Here's a tidyverse way to accomplish the same.
> df<-tibble(myCol=c(5, 4, NA))
> df
# A tibble: 3 x 1
myCol
<dbl>
1 5
2 4
3 NA
> df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
# A tibble: 3 x 1
myCol
<chr>
1 NOTMISSING
2 NOTMISSING
3 <NA>
$endgroup$
add a comment |
$begingroup$
From the documentation of is.numeric:
The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.
So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.
is.numeric(c(5, 4, 3))
[1] TRUE
is.numeric(c(5, 4, NA))
[1] TRUE
That's why either all or none of the values are changed to NOTMISSING.
@eg-r's fix is correct. Here's a tidyverse way to accomplish the same.
> df<-tibble(myCol=c(5, 4, NA))
> df
# A tibble: 3 x 1
myCol
<dbl>
1 5
2 4
3 NA
> df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
# A tibble: 3 x 1
myCol
<chr>
1 NOTMISSING
2 NOTMISSING
3 <NA>
$endgroup$
From the documentation of is.numeric:
The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.
So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.
is.numeric(c(5, 4, 3))
[1] TRUE
is.numeric(c(5, 4, NA))
[1] TRUE
That's why either all or none of the values are changed to NOTMISSING.
@eg-r's fix is correct. Here's a tidyverse way to accomplish the same.
> df<-tibble(myCol=c(5, 4, NA))
> df
# A tibble: 3 x 1
myCol
<dbl>
1 5
2 4
3 NA
> df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
# A tibble: 3 x 1
myCol
<chr>
1 NOTMISSING
2 NOTMISSING
3 <NA>
answered Nov 23 '18 at 17:18
John RauserJohn Rauser
1014
1014
add a comment |
add a comment |
$begingroup$
NAs can be numeric (especially if the other values in that column are all numeric). Try this :
df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)
Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :
df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')
$endgroup$
add a comment |
$begingroup$
NAs can be numeric (especially if the other values in that column are all numeric). Try this :
df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)
Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :
df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')
$endgroup$
add a comment |
$begingroup$
NAs can be numeric (especially if the other values in that column are all numeric). Try this :
df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)
Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :
df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')
$endgroup$
NAs can be numeric (especially if the other values in that column are all numeric). Try this :
df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)
Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :
df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')
answered Sep 22 '18 at 0:40
eg-reg-r
16614
16614
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f38497%2fhow-to-i-replace-numeric-values-with-a-string-in-an-r-dataframe%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
$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40
$begingroup$
df[is.numeric(df)]="string".$endgroup$
– user2974951
Sep 20 '18 at 6:31