I am trying to create a function in R that takes four arguments, namely: data frame, number, character 1 and character 2.
What I am trying to have as an output is this:
test_df <- data.frame(col1 = c("matt", "baby"), col2 = c("john", "luck"))
my_function(test_df, 1, "u", "o")
col1 col2
mutt john
buby luck
I was just wondering how should I specifically define the function to take the [number] column the user is entering? For the renaming, I guess the function rename() would be fine. Do I need to substitue with [x,x]?
Thank you!
If you have to create a function that takes a column as an argument you need to split out the data frame and column specification (using gsub()
to do the actual replacement):
my_function <- function(df, column, pattern, replacement) {
gsub(pattern, replacement, df[[column]])
}
Which would work like:
my_function(df = test_df, column = 1, pattern = "a", replacement = "u")
## [1] "mutt" "buby"
But, this has the downside that if you want to loop over multiple columns, for example with lapply()
, the list specification becomes more complicated:
test_df[] <- lapply(colnames(test_df), my_function, df = test_df, pattern = "a", replacement = "u")
test_df
# col1 col2
# 1 mutt john
# 2 buby luck
Which is much more complicated than:
test_df <- data.frame(test_df, stringsAsFactors = FALSE)
test_df[] <- lapply(test_df, gsub, pattern = "a", replacement = "u")
test_df
# col1 col2
# 1 mutt john
# 2 buby luck
(Note: ensure stringsAsFactors = FALSE
for this to work. It's a good idea to use this as the default unless you explicitly want factors anyway)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.