Overwriting the output of an function affecting multiple columns to a data frameDrop factor levels in a subsetted data frameHow to sort a dataframe by multiple column(s)How to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameSplit data frame string column into multiple columnsDrop data frame columns by nameHow to drop columns by name in a data frameChanging column names of a data frameExtracting specific columns from a data frame
What happens if there is no space for entry stamp in the passport for US visa?
Was Apollo 13 radio blackout on reentry longer than expected?
Credit card details stolen every 1-2 years. What am I doing wrong?
Should I be able to keep my company purchased standing desk when I leave my job?
Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"
What details should I consider before agreeing for part of my salary to be 'retained' by employer?
How Can I Process Untrusted Data Sources Securely?
Will this tire fail its MOT?
Alternator dying so junk car?
Kepler space telescope undetected planets
I want to know the name of this below component
How to color a tag in a math equation?
Did 007 exist before James Bond?
Strategy to pay off revolving debt while building reserve savings fund?
Where did "a racist bone in [one's] body" and "a mean bone in [one's] body" come from?
Why does pip3 install in ~/.local on Debian?
Did Voldemort kill his father before finding out about Horcruxes?
What impact would a dragon the size of Asia have on the environment?
How to make a plagal cadence sound convincing as an ending?
Why does FFmpeg choose 10+20+20 ms instead of an even 16 ms for 60 fps GIF images?
What prompted Cuba to fight against South African Imperialism?
Interviewing with an unmentioned 9 months of sick leave taken during a job
Create Array from list of indices/values
How many bits in the resultant hash will change, if the x bits are changed in its the original input?
Overwriting the output of an function affecting multiple columns to a data frame
Drop factor levels in a subsetted data frameHow to sort a dataframe by multiple column(s)How to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameSplit data frame string column into multiple columnsDrop data frame columns by nameHow to drop columns by name in a data frameChanging column names of a data frameExtracting specific columns from a data frame
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a large data frame (travel) containing many individual travel histories. In the travel$Travel 1 Destination column I want only country data, but numerous entries have region, city or other data entered.
I have tried to write a function (city_replace_with_country) with 2 arguments:
- city_name: the name of the city already in the travel$`Travel 1 Destination' column
- country_name: the name of the country it should be
I would like my function to find these errors in the data frame, paste them into another pre-existing column (travel$regions_visited), and replace the city/region name in the travel$`Travel 1 Destination' with the correct country name.
This code works fine for individual examples eg. bangalore and india
for (i in 1:nrow(travel) )
if(grepl("bangalore", travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], "Bangalore", sep = " ")
travel$`Travel 1 Destination`[i] <- gsub("bangalore", "india", travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
Ideally I would be able to make many corrections to the data frame using the function
city_replace_with_country <- function(city_name, country_name)
for (i in 1:nrow(travel))
if(grepl(city_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], city_name, sep = " ")
travel$`Travel 1 Destination`[i] <- gsub(city_name, country_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
When I try and use this function as follows:
city_replace_with_country("bangalore", "india")
The output of the function doesn't seem to write/store to the data frame.
If I try:
travel <- city_replace_with_country("bangalore", "india")
or
travel$`Travel 1 Destination` <- city_replace_with_country("bangalore", "india")
It returns a NULL value.
Any suggestions as to how to get this to work would be greatly appreciated. Many thanks.
Here is some example data. In this case for example, I would want to change Bogota for Colombia:
travel_example <- structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21), `Travel 1 Dates` = c("03/09/18 to 02/10/18",
NA, "15/08/18 - 24/08/18", "13/09/2018 to 19/09/2018", "15/07- 14/08",
"21/9/18-29/9/18", "10/09/18-3/10/18", "28/5/18-31/7/18", "1/9/18-16/9/18",
NA, NA, NA, "15/07/18-03/10/18", "15/09/18-30/09/18", "18/09/18-05/10/18",
"7/9/18-23/9/18", "14/8/18-11/9/18", "25/7/18-13/8/18", "24/9/18-30/9/18",
NA, "18/9/18-2/10/18"), `Travel 1 Destination` = c("Colombia",
"salvador, Bogata, Honduras", "China,Cambodia", "Lagos, Nigeria",
"Uganda", "Indonesia", "Kenya", "Dubai, Japan", "Sri Lanka",
"Jakarta,Indonesia", "South Africa, Mozambique, Ethiopia", NA,
"Ukraine, Slovakia, India", "Tanzania", "Ghana", "Sri Lanka",
"Angola", "Tajikistan , Afghanistan", "Morocco", NA, "Tanzania"
), regions_visited = c("character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "Nairobi", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"Luanda only", "c("south", "north", "very rural")", "character(0)",
"character(0)", "character(0)")), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `Travel 1 Dates` = structure(list(), class = c("collector_character",
"collector")), `Travel 1 Destination` = structure(list(), class = c("collector_character",
"collector")), regions_visited = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
r function
add a comment |
I have a large data frame (travel) containing many individual travel histories. In the travel$Travel 1 Destination column I want only country data, but numerous entries have region, city or other data entered.
I have tried to write a function (city_replace_with_country) with 2 arguments:
- city_name: the name of the city already in the travel$`Travel 1 Destination' column
- country_name: the name of the country it should be
I would like my function to find these errors in the data frame, paste them into another pre-existing column (travel$regions_visited), and replace the city/region name in the travel$`Travel 1 Destination' with the correct country name.
This code works fine for individual examples eg. bangalore and india
for (i in 1:nrow(travel) )
if(grepl("bangalore", travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], "Bangalore", sep = " ")
travel$`Travel 1 Destination`[i] <- gsub("bangalore", "india", travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
Ideally I would be able to make many corrections to the data frame using the function
city_replace_with_country <- function(city_name, country_name)
for (i in 1:nrow(travel))
if(grepl(city_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], city_name, sep = " ")
travel$`Travel 1 Destination`[i] <- gsub(city_name, country_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
When I try and use this function as follows:
city_replace_with_country("bangalore", "india")
The output of the function doesn't seem to write/store to the data frame.
If I try:
travel <- city_replace_with_country("bangalore", "india")
or
travel$`Travel 1 Destination` <- city_replace_with_country("bangalore", "india")
It returns a NULL value.
Any suggestions as to how to get this to work would be greatly appreciated. Many thanks.
Here is some example data. In this case for example, I would want to change Bogota for Colombia:
travel_example <- structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21), `Travel 1 Dates` = c("03/09/18 to 02/10/18",
NA, "15/08/18 - 24/08/18", "13/09/2018 to 19/09/2018", "15/07- 14/08",
"21/9/18-29/9/18", "10/09/18-3/10/18", "28/5/18-31/7/18", "1/9/18-16/9/18",
NA, NA, NA, "15/07/18-03/10/18", "15/09/18-30/09/18", "18/09/18-05/10/18",
"7/9/18-23/9/18", "14/8/18-11/9/18", "25/7/18-13/8/18", "24/9/18-30/9/18",
NA, "18/9/18-2/10/18"), `Travel 1 Destination` = c("Colombia",
"salvador, Bogata, Honduras", "China,Cambodia", "Lagos, Nigeria",
"Uganda", "Indonesia", "Kenya", "Dubai, Japan", "Sri Lanka",
"Jakarta,Indonesia", "South Africa, Mozambique, Ethiopia", NA,
"Ukraine, Slovakia, India", "Tanzania", "Ghana", "Sri Lanka",
"Angola", "Tajikistan , Afghanistan", "Morocco", NA, "Tanzania"
), regions_visited = c("character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "Nairobi", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"Luanda only", "c("south", "north", "very rural")", "character(0)",
"character(0)", "character(0)")), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `Travel 1 Dates` = structure(list(), class = c("collector_character",
"collector")), `Travel 1 Destination` = structure(list(), class = c("collector_character",
"collector")), regions_visited = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
r function
Please share sample of the dataframe withdput
– Sonny
Mar 26 at 9:03
Thanks. Added a sample.
– tommy
Mar 26 at 9:53
Sorry, have just corrected the dput output (originally parsed with read.csv, when should have been read_csv).
– tommy
Mar 26 at 10:00
add a comment |
I have a large data frame (travel) containing many individual travel histories. In the travel$Travel 1 Destination column I want only country data, but numerous entries have region, city or other data entered.
I have tried to write a function (city_replace_with_country) with 2 arguments:
- city_name: the name of the city already in the travel$`Travel 1 Destination' column
- country_name: the name of the country it should be
I would like my function to find these errors in the data frame, paste them into another pre-existing column (travel$regions_visited), and replace the city/region name in the travel$`Travel 1 Destination' with the correct country name.
This code works fine for individual examples eg. bangalore and india
for (i in 1:nrow(travel) )
if(grepl("bangalore", travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], "Bangalore", sep = " ")
travel$`Travel 1 Destination`[i] <- gsub("bangalore", "india", travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
Ideally I would be able to make many corrections to the data frame using the function
city_replace_with_country <- function(city_name, country_name)
for (i in 1:nrow(travel))
if(grepl(city_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], city_name, sep = " ")
travel$`Travel 1 Destination`[i] <- gsub(city_name, country_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
When I try and use this function as follows:
city_replace_with_country("bangalore", "india")
The output of the function doesn't seem to write/store to the data frame.
If I try:
travel <- city_replace_with_country("bangalore", "india")
or
travel$`Travel 1 Destination` <- city_replace_with_country("bangalore", "india")
It returns a NULL value.
Any suggestions as to how to get this to work would be greatly appreciated. Many thanks.
Here is some example data. In this case for example, I would want to change Bogota for Colombia:
travel_example <- structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21), `Travel 1 Dates` = c("03/09/18 to 02/10/18",
NA, "15/08/18 - 24/08/18", "13/09/2018 to 19/09/2018", "15/07- 14/08",
"21/9/18-29/9/18", "10/09/18-3/10/18", "28/5/18-31/7/18", "1/9/18-16/9/18",
NA, NA, NA, "15/07/18-03/10/18", "15/09/18-30/09/18", "18/09/18-05/10/18",
"7/9/18-23/9/18", "14/8/18-11/9/18", "25/7/18-13/8/18", "24/9/18-30/9/18",
NA, "18/9/18-2/10/18"), `Travel 1 Destination` = c("Colombia",
"salvador, Bogata, Honduras", "China,Cambodia", "Lagos, Nigeria",
"Uganda", "Indonesia", "Kenya", "Dubai, Japan", "Sri Lanka",
"Jakarta,Indonesia", "South Africa, Mozambique, Ethiopia", NA,
"Ukraine, Slovakia, India", "Tanzania", "Ghana", "Sri Lanka",
"Angola", "Tajikistan , Afghanistan", "Morocco", NA, "Tanzania"
), regions_visited = c("character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "Nairobi", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"Luanda only", "c("south", "north", "very rural")", "character(0)",
"character(0)", "character(0)")), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `Travel 1 Dates` = structure(list(), class = c("collector_character",
"collector")), `Travel 1 Destination` = structure(list(), class = c("collector_character",
"collector")), regions_visited = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
r function
I have a large data frame (travel) containing many individual travel histories. In the travel$Travel 1 Destination column I want only country data, but numerous entries have region, city or other data entered.
I have tried to write a function (city_replace_with_country) with 2 arguments:
- city_name: the name of the city already in the travel$`Travel 1 Destination' column
- country_name: the name of the country it should be
I would like my function to find these errors in the data frame, paste them into another pre-existing column (travel$regions_visited), and replace the city/region name in the travel$`Travel 1 Destination' with the correct country name.
This code works fine for individual examples eg. bangalore and india
for (i in 1:nrow(travel) )
if(grepl("bangalore", travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], "Bangalore", sep = " ")
travel$`Travel 1 Destination`[i] <- gsub("bangalore", "india", travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
Ideally I would be able to make many corrections to the data frame using the function
city_replace_with_country <- function(city_name, country_name)
for (i in 1:nrow(travel))
if(grepl(city_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE))
travel$regions_visited[i] <- paste(travel$regions_visited[i], city_name, sep = " ")
travel$`Travel 1 Destination`[i] <- gsub(city_name, country_name, travel$`Travel 1 Destination`[i], ignore.case = TRUE, perl = TRUE)
When I try and use this function as follows:
city_replace_with_country("bangalore", "india")
The output of the function doesn't seem to write/store to the data frame.
If I try:
travel <- city_replace_with_country("bangalore", "india")
or
travel$`Travel 1 Destination` <- city_replace_with_country("bangalore", "india")
It returns a NULL value.
Any suggestions as to how to get this to work would be greatly appreciated. Many thanks.
Here is some example data. In this case for example, I would want to change Bogota for Colombia:
travel_example <- structure(list(X1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21), `Travel 1 Dates` = c("03/09/18 to 02/10/18",
NA, "15/08/18 - 24/08/18", "13/09/2018 to 19/09/2018", "15/07- 14/08",
"21/9/18-29/9/18", "10/09/18-3/10/18", "28/5/18-31/7/18", "1/9/18-16/9/18",
NA, NA, NA, "15/07/18-03/10/18", "15/09/18-30/09/18", "18/09/18-05/10/18",
"7/9/18-23/9/18", "14/8/18-11/9/18", "25/7/18-13/8/18", "24/9/18-30/9/18",
NA, "18/9/18-2/10/18"), `Travel 1 Destination` = c("Colombia",
"salvador, Bogata, Honduras", "China,Cambodia", "Lagos, Nigeria",
"Uganda", "Indonesia", "Kenya", "Dubai, Japan", "Sri Lanka",
"Jakarta,Indonesia", "South Africa, Mozambique, Ethiopia", NA,
"Ukraine, Slovakia, India", "Tanzania", "Ghana", "Sri Lanka",
"Angola", "Tajikistan , Afghanistan", "Morocco", NA, "Tanzania"
), regions_visited = c("character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "Nairobi", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"character(0)", "character(0)", "character(0)", "character(0)",
"Luanda only", "c("south", "north", "very rural")", "character(0)",
"character(0)", "character(0)")), class = c("spec_tbl_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -21L), spec = structure(list(
cols = list(X1 = structure(list(), class = c("collector_double",
"collector")), `Travel 1 Dates` = structure(list(), class = c("collector_character",
"collector")), `Travel 1 Destination` = structure(list(), class = c("collector_character",
"collector")), regions_visited = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
r function
r function
edited Mar 26 at 15:02
Matt Summersgill
2,3695 silver badges29 bronze badges
2,3695 silver badges29 bronze badges
asked Mar 26 at 9:01
tommytommy
11 bronze badge
11 bronze badge
Please share sample of the dataframe withdput
– Sonny
Mar 26 at 9:03
Thanks. Added a sample.
– tommy
Mar 26 at 9:53
Sorry, have just corrected the dput output (originally parsed with read.csv, when should have been read_csv).
– tommy
Mar 26 at 10:00
add a comment |
Please share sample of the dataframe withdput
– Sonny
Mar 26 at 9:03
Thanks. Added a sample.
– tommy
Mar 26 at 9:53
Sorry, have just corrected the dput output (originally parsed with read.csv, when should have been read_csv).
– tommy
Mar 26 at 10:00
Please share sample of the dataframe with
dput– Sonny
Mar 26 at 9:03
Please share sample of the dataframe with
dput– Sonny
Mar 26 at 9:03
Thanks. Added a sample.
– tommy
Mar 26 at 9:53
Thanks. Added a sample.
– tommy
Mar 26 at 9:53
Sorry, have just corrected the dput output (originally parsed with read.csv, when should have been read_csv).
– tommy
Mar 26 at 10:00
Sorry, have just corrected the dput output (originally parsed with read.csv, when should have been read_csv).
– tommy
Mar 26 at 10:00
add a comment |
1 Answer
1
active
oldest
votes
In your case, you could just extract the last word after comma using regex and get all the country names as below:
travel_example$`Travel 1 Destination` <- sub('.*,\s*', '', travel_example$`Travel 1 Destination`)
travel_example$`Travel 1 Destination`
[1] "Colombia" "Honduras" "Cambodia" "Nigeria" "Uganda"
[6] "Indonesia" "Kenya" "Japan" "Sri Lanka" "Indonesia"
[11] "Ethiopia" NA "India" "Tanzania" "Ghana"
[16] "Sri Lanka" "Angola" "Afghanistan" "Morocco" NA
[21] "Tanzania"
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%2f55353224%2foverwriting-the-output-of-an-function-affecting-multiple-columns-to-a-data-frame%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
In your case, you could just extract the last word after comma using regex and get all the country names as below:
travel_example$`Travel 1 Destination` <- sub('.*,\s*', '', travel_example$`Travel 1 Destination`)
travel_example$`Travel 1 Destination`
[1] "Colombia" "Honduras" "Cambodia" "Nigeria" "Uganda"
[6] "Indonesia" "Kenya" "Japan" "Sri Lanka" "Indonesia"
[11] "Ethiopia" NA "India" "Tanzania" "Ghana"
[16] "Sri Lanka" "Angola" "Afghanistan" "Morocco" NA
[21] "Tanzania"
add a comment |
In your case, you could just extract the last word after comma using regex and get all the country names as below:
travel_example$`Travel 1 Destination` <- sub('.*,\s*', '', travel_example$`Travel 1 Destination`)
travel_example$`Travel 1 Destination`
[1] "Colombia" "Honduras" "Cambodia" "Nigeria" "Uganda"
[6] "Indonesia" "Kenya" "Japan" "Sri Lanka" "Indonesia"
[11] "Ethiopia" NA "India" "Tanzania" "Ghana"
[16] "Sri Lanka" "Angola" "Afghanistan" "Morocco" NA
[21] "Tanzania"
add a comment |
In your case, you could just extract the last word after comma using regex and get all the country names as below:
travel_example$`Travel 1 Destination` <- sub('.*,\s*', '', travel_example$`Travel 1 Destination`)
travel_example$`Travel 1 Destination`
[1] "Colombia" "Honduras" "Cambodia" "Nigeria" "Uganda"
[6] "Indonesia" "Kenya" "Japan" "Sri Lanka" "Indonesia"
[11] "Ethiopia" NA "India" "Tanzania" "Ghana"
[16] "Sri Lanka" "Angola" "Afghanistan" "Morocco" NA
[21] "Tanzania"
In your case, you could just extract the last word after comma using regex and get all the country names as below:
travel_example$`Travel 1 Destination` <- sub('.*,\s*', '', travel_example$`Travel 1 Destination`)
travel_example$`Travel 1 Destination`
[1] "Colombia" "Honduras" "Cambodia" "Nigeria" "Uganda"
[6] "Indonesia" "Kenya" "Japan" "Sri Lanka" "Indonesia"
[11] "Ethiopia" NA "India" "Tanzania" "Ghana"
[16] "Sri Lanka" "Angola" "Afghanistan" "Morocco" NA
[21] "Tanzania"
answered Mar 26 at 10:07
SonnySonny
2,6901 gold badge6 silver badges17 bronze badges
2,6901 gold badge6 silver badges17 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55353224%2foverwriting-the-output-of-an-function-affecting-multiple-columns-to-a-data-frame%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
Please share sample of the dataframe with
dput– Sonny
Mar 26 at 9:03
Thanks. Added a sample.
– tommy
Mar 26 at 9:53
Sorry, have just corrected the dput output (originally parsed with read.csv, when should have been read_csv).
– tommy
Mar 26 at 10:00