Reputation: 347
I have about 12 datasets that I am trying to clean before implementing them as datatables in my final code. I currently have code that works for individual datasets but I want to implement this as a function so that I don't have to re-type the code blocks over and over.
countyshapesClean <- function(inputdata, outputdata) {
outputdata <<- inputdata %>%
select(
county_cl,
state_abb,
avg_opioid,
avg_oxy_hydro,
avg_opioid_perc,
avg_oxy_hydro_perc,
avg_opioid_ppp,
avg_oxy_hydro_ppp
) %>%
mutate(
avg_opioid = format(round(as.numeric(avg_opioid),2), nsmall = 2, big.mark = ","),
avg_oxy_hydro = format(round(as.numeric(avg_oxy_hydro),2), nsmall = 2, big.mark = ","),
avg_opioid_perc = format(round(as.numeric(avg_opioid_perc),2), nsmall = 2, big.mark = ","),
avg_oxy_hydro_perc = format(round(as.numeric(avg_oxy_hydro_perc),2), nsmall = 2, big.mark = ","),
avg_opioid_ppp = format(round(as.numeric(avg_opioid_ppp),2), nsmall = 2, big.mark = ","),
avg_oxy_hydro_ppp = format(round(as.numeric(avg_oxy_hydro_ppp),2), nsmall = 2, big.mark = ",")
)
}
countyshapesClean(inputdata = countymerge2006, outputdata = countymerge2006clean)
This works but my output dataset is named outputdata
instead of countymerge2006clean
. How would I change this code to get my desired result?
Upvotes: 1
Views: 42
Reputation: 206197
Don't pass in outputdata
to the function. Instead, do something like
countymerge2006clean <- countyshapesClean(inputdata = countymerge2006)
where your function returns the updated data.frame.
countyshapesClean <- function(inputdata) {
inputdata %>%
select(...) %>%
mutate(...)
}
Proper functions in R should not create variables outside their own scope. (It's not considered good practice to use <<-
).
Of course, almost anything is possible is R, and you could create variables outside the function scope with the assign()
function (though I strongly recommend you do NOT do this). That would look something like this
countyshapesClean <- function(inputdata, outputdata) {
outvar <- rlang::ensym(outputdata)
new <- inputdata %>%
select(...) %>%
mutate(...)
assign(rlang::quo_name(outvar), new, parent.frame())
}
countyshapesClean(inputdata = countymerge2006, outputdata = countymerge2006clean)
Upvotes: 2