Changing Values in a column based on criteria in R Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I quickly rename a MySQL database (change schema name)?How to sort a dataframe by multiple column(s)How to sum a variable by groupEasiest way to rename a model using Django/South?Renaming columns in pandasCall apply-like function on each row of dataframe with multiple arguments from each rowRename multiple columns by namesHow to rename columns of R dataframe based on some existing pattern?Change values of multiple columns based on the value of one column in data.tableChange column name based on row values
Google .dev domain strangely redirects to https
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
What to do with repeated rejections for phd position
An adverb for when you're not exaggerating
What are the discoveries that have been possible with the rejection of positivism?
Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?
What does it mean that physics no longer uses mechanical models to describe phenomena?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
Tannaka duality for semisimple groups
A term for a woman complaining about things/begging in a cute/childish way
What is the meaning of 'breadth' in breadth first search?
If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?
Why are my pictures showing a dark band on one edge?
What order were files/directories output in dir?
What does this say in Elvish?
1-probability to calculate two events in a row
What does Turing mean by this statement?
Why weren't discrete x86 CPUs ever used in game hardware?
Flash light on something
What does 丫 mean? 丫是什么意思?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
How to compare two different files line by line in unix?
Getting prompted for verification code but where do I put it in?
How would a mousetrap for use in space work?
Changing Values in a column based on criteria in R
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I quickly rename a MySQL database (change schema name)?How to sort a dataframe by multiple column(s)How to sum a variable by groupEasiest way to rename a model using Django/South?Renaming columns in pandasCall apply-like function on each row of dataframe with multiple arguments from each rowRename multiple columns by namesHow to rename columns of R dataframe based on some existing pattern?Change values of multiple columns based on the value of one column in data.tableChange column name based on row values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.
I tried to make a for loop but it wouldn't work. Maybe there is an easier way?
r rename
add a comment |
Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.
I tried to make a for loop but it wouldn't work. Maybe there is an easier way?
r rename
Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask
– DJV
Mar 8 at 21:56
add a comment |
Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.
I tried to make a for loop but it wouldn't work. Maybe there is an easier way?
r rename
Let's say I have a column data$code with values like 1234, 222F, 456Q, 1234, 1234...
Now I want to rename them so that every occurrence of 1234 would be 'Model 1', every occerence of 456Q -> 'Model X' etc.
I tried to make a for loop but it wouldn't work. Maybe there is an easier way?
r rename
r rename
asked Mar 8 at 21:53
koraskoras
53
53
Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask
– DJV
Mar 8 at 21:56
add a comment |
Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask
– DJV
Mar 8 at 21:56
Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask
– DJV
Mar 8 at 21:56
Welcome to SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask
– DJV
Mar 8 at 21:56
add a comment |
1 Answer
1
active
oldest
votes
from you example I assume that code
is a categorical variable, therefore what you need to do is change a value of the levels for that variable.
For example:
id <- c(1,2,3,4,5)
code <- c('1234', '222F', '456Q', '1234', '1234')
data <- data.frame(id, code)
data
id code
1 1 1234
2 2 222F
3 3 456Q
4 4 1234
5 5 1234
levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
data
id code
1 1 Model 1
2 2 222F
3 3 Model X
4 4 Model 1
5 5 Model 1
You simply have to repeat for every level you need to change.
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%2f55071517%2fchanging-values-in-a-column-based-on-criteria-in-r%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
from you example I assume that code
is a categorical variable, therefore what you need to do is change a value of the levels for that variable.
For example:
id <- c(1,2,3,4,5)
code <- c('1234', '222F', '456Q', '1234', '1234')
data <- data.frame(id, code)
data
id code
1 1 1234
2 2 222F
3 3 456Q
4 4 1234
5 5 1234
levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
data
id code
1 1 Model 1
2 2 222F
3 3 Model X
4 4 Model 1
5 5 Model 1
You simply have to repeat for every level you need to change.
add a comment |
from you example I assume that code
is a categorical variable, therefore what you need to do is change a value of the levels for that variable.
For example:
id <- c(1,2,3,4,5)
code <- c('1234', '222F', '456Q', '1234', '1234')
data <- data.frame(id, code)
data
id code
1 1 1234
2 2 222F
3 3 456Q
4 4 1234
5 5 1234
levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
data
id code
1 1 Model 1
2 2 222F
3 3 Model X
4 4 Model 1
5 5 Model 1
You simply have to repeat for every level you need to change.
add a comment |
from you example I assume that code
is a categorical variable, therefore what you need to do is change a value of the levels for that variable.
For example:
id <- c(1,2,3,4,5)
code <- c('1234', '222F', '456Q', '1234', '1234')
data <- data.frame(id, code)
data
id code
1 1 1234
2 2 222F
3 3 456Q
4 4 1234
5 5 1234
levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
data
id code
1 1 Model 1
2 2 222F
3 3 Model X
4 4 Model 1
5 5 Model 1
You simply have to repeat for every level you need to change.
from you example I assume that code
is a categorical variable, therefore what you need to do is change a value of the levels for that variable.
For example:
id <- c(1,2,3,4,5)
code <- c('1234', '222F', '456Q', '1234', '1234')
data <- data.frame(id, code)
data
id code
1 1 1234
2 2 222F
3 3 456Q
4 4 1234
5 5 1234
levels(data$code)[levels(data$code)=='1234'] <- 'Model 1'
levels(data$code)[levels(data$code)=='456Q'] <- 'Model X'
data
id code
1 1 Model 1
2 2 222F
3 3 Model X
4 4 Model 1
5 5 Model 1
You simply have to repeat for every level you need to change.
answered Mar 9 at 2:44
alessioalessio
37427
37427
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%2f55071517%2fchanging-values-in-a-column-based-on-criteria-in-r%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 SO! please look at the link. It will be easier for us to help you. stackoverflow.com/help/how-to-ask
– DJV
Mar 8 at 21:56