change argument object in R












0














I want to mutate a data.frame object within a function. The following does not do what I intended:



# function to change factors to characters using dplyr
# x: a data.frame
fa_clean <- function(x,...) {
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
print(x)
return(x)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))

fa_clean(test) # applying the function
str(test) # letter is still a factor ???


I am obviously missing something. Edit: And I am aware of:



test <- fa_clean(test)


But I would like to run it without this assignment.










share|improve this question
























  • R is a functional language. It's a feature that you have to explicitly assign the result to something.
    – Hong Ooi
    Nov 23 '18 at 8:54










  • Also, printing something to the console is not the same as returning a result
    – Hong Ooi
    Nov 23 '18 at 8:55






  • 1




    Would magrittr's inplace pipe work for your needs? test %<>% mutate_if(is.factor, as.character) will update test in place, although if you use that inside a function with argument x, it won't update test outside of the function.
    – andrew_reece
    Nov 23 '18 at 8:57
















0














I want to mutate a data.frame object within a function. The following does not do what I intended:



# function to change factors to characters using dplyr
# x: a data.frame
fa_clean <- function(x,...) {
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
print(x)
return(x)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))

fa_clean(test) # applying the function
str(test) # letter is still a factor ???


I am obviously missing something. Edit: And I am aware of:



test <- fa_clean(test)


But I would like to run it without this assignment.










share|improve this question
























  • R is a functional language. It's a feature that you have to explicitly assign the result to something.
    – Hong Ooi
    Nov 23 '18 at 8:54










  • Also, printing something to the console is not the same as returning a result
    – Hong Ooi
    Nov 23 '18 at 8:55






  • 1




    Would magrittr's inplace pipe work for your needs? test %<>% mutate_if(is.factor, as.character) will update test in place, although if you use that inside a function with argument x, it won't update test outside of the function.
    – andrew_reece
    Nov 23 '18 at 8:57














0












0








0







I want to mutate a data.frame object within a function. The following does not do what I intended:



# function to change factors to characters using dplyr
# x: a data.frame
fa_clean <- function(x,...) {
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
print(x)
return(x)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))

fa_clean(test) # applying the function
str(test) # letter is still a factor ???


I am obviously missing something. Edit: And I am aware of:



test <- fa_clean(test)


But I would like to run it without this assignment.










share|improve this question















I want to mutate a data.frame object within a function. The following does not do what I intended:



# function to change factors to characters using dplyr
# x: a data.frame
fa_clean <- function(x,...) {
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
print(x)
return(x)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))

fa_clean(test) # applying the function
str(test) # letter is still a factor ???


I am obviously missing something. Edit: And I am aware of:



test <- fa_clean(test)


But I would like to run it without this assignment.







r function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 10:40

























asked Nov 23 '18 at 8:30









Krisselack

10010




10010












  • R is a functional language. It's a feature that you have to explicitly assign the result to something.
    – Hong Ooi
    Nov 23 '18 at 8:54










  • Also, printing something to the console is not the same as returning a result
    – Hong Ooi
    Nov 23 '18 at 8:55






  • 1




    Would magrittr's inplace pipe work for your needs? test %<>% mutate_if(is.factor, as.character) will update test in place, although if you use that inside a function with argument x, it won't update test outside of the function.
    – andrew_reece
    Nov 23 '18 at 8:57


















  • R is a functional language. It's a feature that you have to explicitly assign the result to something.
    – Hong Ooi
    Nov 23 '18 at 8:54










  • Also, printing something to the console is not the same as returning a result
    – Hong Ooi
    Nov 23 '18 at 8:55






  • 1




    Would magrittr's inplace pipe work for your needs? test %<>% mutate_if(is.factor, as.character) will update test in place, although if you use that inside a function with argument x, it won't update test outside of the function.
    – andrew_reece
    Nov 23 '18 at 8:57
















R is a functional language. It's a feature that you have to explicitly assign the result to something.
– Hong Ooi
Nov 23 '18 at 8:54




R is a functional language. It's a feature that you have to explicitly assign the result to something.
– Hong Ooi
Nov 23 '18 at 8:54












Also, printing something to the console is not the same as returning a result
– Hong Ooi
Nov 23 '18 at 8:55




Also, printing something to the console is not the same as returning a result
– Hong Ooi
Nov 23 '18 at 8:55




1




1




Would magrittr's inplace pipe work for your needs? test %<>% mutate_if(is.factor, as.character) will update test in place, although if you use that inside a function with argument x, it won't update test outside of the function.
– andrew_reece
Nov 23 '18 at 8:57




Would magrittr's inplace pipe work for your needs? test %<>% mutate_if(is.factor, as.character) will update test in place, although if you use that inside a function with argument x, it won't update test outside of the function.
– andrew_reece
Nov 23 '18 at 8:57












1 Answer
1






active

oldest

votes


















1














Here is your code with the necessary modification to make it work:



fa_clean <- function(x) {
varname <- deparse(substitute(x))
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
assign(varname, x, envir = .GlobalEnv)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))
fa_clean(test)
str(test) # letter is no longer a factor
'data.frame': 10 obs. of 2 variables:
$ number: int 1 2 3 4 5 6 7 8 9 10
$ letter: chr "a" "b" "c" "d" ...





share|improve this answer























  • Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
    – Krisselack
    Nov 23 '18 at 10:32












  • fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
    – Krisselack
    Nov 23 '18 at 10:35








  • 1




    This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
    – Krisselack
    Nov 23 '18 at 10:51











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443065%2fchange-argument-object-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









1














Here is your code with the necessary modification to make it work:



fa_clean <- function(x) {
varname <- deparse(substitute(x))
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
assign(varname, x, envir = .GlobalEnv)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))
fa_clean(test)
str(test) # letter is no longer a factor
'data.frame': 10 obs. of 2 variables:
$ number: int 1 2 3 4 5 6 7 8 9 10
$ letter: chr "a" "b" "c" "d" ...





share|improve this answer























  • Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
    – Krisselack
    Nov 23 '18 at 10:32












  • fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
    – Krisselack
    Nov 23 '18 at 10:35








  • 1




    This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
    – Krisselack
    Nov 23 '18 at 10:51
















1














Here is your code with the necessary modification to make it work:



fa_clean <- function(x) {
varname <- deparse(substitute(x))
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
assign(varname, x, envir = .GlobalEnv)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))
fa_clean(test)
str(test) # letter is no longer a factor
'data.frame': 10 obs. of 2 variables:
$ number: int 1 2 3 4 5 6 7 8 9 10
$ letter: chr "a" "b" "c" "d" ...





share|improve this answer























  • Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
    – Krisselack
    Nov 23 '18 at 10:32












  • fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
    – Krisselack
    Nov 23 '18 at 10:35








  • 1




    This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
    – Krisselack
    Nov 23 '18 at 10:51














1












1








1






Here is your code with the necessary modification to make it work:



fa_clean <- function(x) {
varname <- deparse(substitute(x))
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
assign(varname, x, envir = .GlobalEnv)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))
fa_clean(test)
str(test) # letter is no longer a factor
'data.frame': 10 obs. of 2 variables:
$ number: int 1 2 3 4 5 6 7 8 9 10
$ letter: chr "a" "b" "c" "d" ...





share|improve this answer














Here is your code with the necessary modification to make it work:



fa_clean <- function(x) {
varname <- deparse(substitute(x))
require(dplyr)
x <- x %>% mutate_if(is.factor, as.character)
assign(varname, x, envir = .GlobalEnv)
}

# example set
test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10]))
fa_clean(test)
str(test) # letter is no longer a factor
'data.frame': 10 obs. of 2 variables:
$ number: int 1 2 3 4 5 6 7 8 9 10
$ letter: chr "a" "b" "c" "d" ...






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 13 '18 at 10:05

























answered Nov 23 '18 at 9:34









snoram

6,402831




6,402831












  • Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
    – Krisselack
    Nov 23 '18 at 10:32












  • fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
    – Krisselack
    Nov 23 '18 at 10:35








  • 1




    This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
    – Krisselack
    Nov 23 '18 at 10:51


















  • Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
    – Krisselack
    Nov 23 '18 at 10:32












  • fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
    – Krisselack
    Nov 23 '18 at 10:35








  • 1




    This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
    – Krisselack
    Nov 23 '18 at 10:51
















Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
– Krisselack
Nov 23 '18 at 10:32






Thanks, so basically removing the piping did the trick. Your first version, which I also tried before returns a factor.
– Krisselack
Nov 23 '18 at 10:32














fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
– Krisselack
Nov 23 '18 at 10:35






fa_clean <- function(x,...) { require(dplyr) x <- x %>% mutate_if(is.factor, as.character) x # don't print, just return } test <- data.frame(number=c(1:10),letter=as.factor(letters[1:10])) fa_clean(test) str(test) # returns a factor
– Krisselack
Nov 23 '18 at 10:35






1




1




This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
– Krisselack
Nov 23 '18 at 10:51




This is exactly what I wanted. I thought that I would need environments, I just fooled around with <<-, but your solution taught me a lot. Thank you very much.
– Krisselack
Nov 23 '18 at 10:51


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443065%2fchange-argument-object-in-r%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo