Using R how do I change the data in a Column from a Letter to a Word
I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.
How would I do this using R?
Thanks
Paul
r
add a comment |
I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.
How would I do this using R?
Thanks
Paul
r
Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)
– R Yoda
Nov 15 '18 at 18:05
Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?
– R Yoda
Nov 15 '18 at 18:08
add a comment |
I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.
How would I do this using R?
Thanks
Paul
r
I have a column in my spreadsheet called CourseType and the value in the column is either O, OS or C. I want to change the letters to read Online, Onsite.
How would I do this using R?
Thanks
Paul
r
r
asked Nov 15 '18 at 16:40
Paul RameshPaul Ramesh
12
12
Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)
– R Yoda
Nov 15 '18 at 18:05
Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?
– R Yoda
Nov 15 '18 at 18:08
add a comment |
Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)
– R Yoda
Nov 15 '18 at 18:05
Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?
– R Yoda
Nov 15 '18 at 18:08
Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)
– R Yoda
Nov 15 '18 at 18:05
Welcome at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)
– R Yoda
Nov 15 '18 at 18:05
Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?
– R Yoda
Nov 15 '18 at 18:08
Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?
– R Yoda
Nov 15 '18 at 18:08
add a comment |
2 Answers
2
active
oldest
votes
Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse
statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:
df$CourseType_new <- ifelse(df$CourseType == "O", "Online",
ifelse(df$CourseType == "OS", "OnSite", "something else"))
If you have more values, I'd suggest using a lookup table
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
add a comment |
If you want to avoid ifelse
cascades (e. g. in case of many different codes) you can use this approach:
# Construct example data
df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
df
# CourseType teacher
# 1 C Joe
# 2 O Jane
# 3 O Joe
# 4 OS Jane
# 5 OS Joe
# 6 C Jane
# Do recode by filtering the lines
df$CourseType[df$CourseType == "O"] <- "Online"
df$CourseType[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher
# 1 C Joe
# 2 Online Jane
# 3 Online Joe
# 4 OnSite Jane
# 5 OnSite Joe
# 6 C Jane
# Note: The non-matching CourseTypes are left unchanged!
One drawback is that you have no else
part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame
by adding the recoded value to a new column:
df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher CourseTypeRecoded
# 1 C Joe <NA>
# 2 O Jane Online
# 3 O Joe Online
# 4 OS Jane OnSite
# 5 OS Joe OnSite
# 6 C Jane <NA>
You could then set the NA
values (which indicate an recode failure) to a default value, e. g.
df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"
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%2f53324083%2fusing-r-how-do-i-change-the-data-in-a-column-from-a-letter-to-a-word%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
Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse
statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:
df$CourseType_new <- ifelse(df$CourseType == "O", "Online",
ifelse(df$CourseType == "OS", "OnSite", "something else"))
If you have more values, I'd suggest using a lookup table
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
add a comment |
Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse
statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:
df$CourseType_new <- ifelse(df$CourseType == "O", "Online",
ifelse(df$CourseType == "OS", "OnSite", "something else"))
If you have more values, I'd suggest using a lookup table
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
add a comment |
Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse
statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:
df$CourseType_new <- ifelse(df$CourseType == "O", "Online",
ifelse(df$CourseType == "OS", "OnSite", "something else"))
If you have more values, I'd suggest using a lookup table
Assuming the dataframe is called df and the column is named CourseType, you can use an ifelse
statement if the options to be replaced are limited. This is similar to the nested IF-ELSE in excel:
df$CourseType_new <- ifelse(df$CourseType == "O", "Online",
ifelse(df$CourseType == "OS", "OnSite", "something else"))
If you have more values, I'd suggest using a lookup table
answered Nov 15 '18 at 16:47
SmitMSmitM
1,0961211
1,0961211
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
add a comment |
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
Thanks, I will try this.
– Paul Ramesh
Nov 15 '18 at 17:07
add a comment |
If you want to avoid ifelse
cascades (e. g. in case of many different codes) you can use this approach:
# Construct example data
df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
df
# CourseType teacher
# 1 C Joe
# 2 O Jane
# 3 O Joe
# 4 OS Jane
# 5 OS Joe
# 6 C Jane
# Do recode by filtering the lines
df$CourseType[df$CourseType == "O"] <- "Online"
df$CourseType[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher
# 1 C Joe
# 2 Online Jane
# 3 Online Joe
# 4 OnSite Jane
# 5 OnSite Joe
# 6 C Jane
# Note: The non-matching CourseTypes are left unchanged!
One drawback is that you have no else
part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame
by adding the recoded value to a new column:
df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher CourseTypeRecoded
# 1 C Joe <NA>
# 2 O Jane Online
# 3 O Joe Online
# 4 OS Jane OnSite
# 5 OS Joe OnSite
# 6 C Jane <NA>
You could then set the NA
values (which indicate an recode failure) to a default value, e. g.
df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"
add a comment |
If you want to avoid ifelse
cascades (e. g. in case of many different codes) you can use this approach:
# Construct example data
df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
df
# CourseType teacher
# 1 C Joe
# 2 O Jane
# 3 O Joe
# 4 OS Jane
# 5 OS Joe
# 6 C Jane
# Do recode by filtering the lines
df$CourseType[df$CourseType == "O"] <- "Online"
df$CourseType[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher
# 1 C Joe
# 2 Online Jane
# 3 Online Joe
# 4 OnSite Jane
# 5 OnSite Joe
# 6 C Jane
# Note: The non-matching CourseTypes are left unchanged!
One drawback is that you have no else
part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame
by adding the recoded value to a new column:
df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher CourseTypeRecoded
# 1 C Joe <NA>
# 2 O Jane Online
# 3 O Joe Online
# 4 OS Jane OnSite
# 5 OS Joe OnSite
# 6 C Jane <NA>
You could then set the NA
values (which indicate an recode failure) to a default value, e. g.
df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"
add a comment |
If you want to avoid ifelse
cascades (e. g. in case of many different codes) you can use this approach:
# Construct example data
df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
df
# CourseType teacher
# 1 C Joe
# 2 O Jane
# 3 O Joe
# 4 OS Jane
# 5 OS Joe
# 6 C Jane
# Do recode by filtering the lines
df$CourseType[df$CourseType == "O"] <- "Online"
df$CourseType[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher
# 1 C Joe
# 2 Online Jane
# 3 Online Joe
# 4 OnSite Jane
# 5 OnSite Joe
# 6 C Jane
# Note: The non-matching CourseTypes are left unchanged!
One drawback is that you have no else
part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame
by adding the recoded value to a new column:
df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher CourseTypeRecoded
# 1 C Joe <NA>
# 2 O Jane Online
# 3 O Joe Online
# 4 OS Jane OnSite
# 5 OS Joe OnSite
# 6 C Jane <NA>
You could then set the NA
values (which indicate an recode failure) to a default value, e. g.
df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"
If you want to avoid ifelse
cascades (e. g. in case of many different codes) you can use this approach:
# Construct example data
df <- data.frame(CourseType = c("C", "O", "O", "OS", "OS", "C"), teacher = c("Joe", "Jane"), stringsAsFactors = FALSE)
df
# CourseType teacher
# 1 C Joe
# 2 O Jane
# 3 O Joe
# 4 OS Jane
# 5 OS Joe
# 6 C Jane
# Do recode by filtering the lines
df$CourseType[df$CourseType == "O"] <- "Online"
df$CourseType[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher
# 1 C Joe
# 2 Online Jane
# 3 Online Joe
# 4 OnSite Jane
# 5 OnSite Joe
# 6 C Jane
# Note: The non-matching CourseTypes are left unchanged!
One drawback is that you have no else
part where you can set the non-recodes values to a default value... So a better approach would be to "enrich" your data.frame
by adding the recoded value to a new column:
df$CourseTypeRecoded[df$CourseType == "O"] <- "Online"
df$CourseTypeRecoded[df$CourseType == "OS"] <- "OnSite"
df
# CourseType teacher CourseTypeRecoded
# 1 C Joe <NA>
# 2 O Jane Online
# 3 O Joe Online
# 4 OS Jane OnSite
# 5 OS Joe OnSite
# 6 C Jane <NA>
You could then set the NA
values (which indicate an recode failure) to a default value, e. g.
df$CourseTypeRecoded[is.na(df$CourseTypeRecoded)] <- "(invalid code)"
edited Nov 15 '18 at 18:53
answered Nov 15 '18 at 18:47
R YodaR Yoda
4,2532041
4,2532041
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%2f53324083%2fusing-r-how-do-i-change-the-data-in-a-column-from-a-letter-to-a-word%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 at SO! Please note that questions shall contain a minimal reproducible example in R that construct example data. Also indicate the expected result for the example data. This makes it easier for us to answer (without wasting time and to be sure to provide what you need). THX :-)
– R Yoda
Nov 15 '18 at 18:05
Do you also (implicitly) ask how to read an Excel file or "just" how to recode column values?
– R Yoda
Nov 15 '18 at 18:08