tidyr using mutate inside a function

0 votes
I'd like to use mutate function from the tidyverse to create a new column based on the old column using only a data frame and strings, which represent column headers, as inputs.

I can get this to work without using the tidyverse (see function f below), but I'd like to get it to work using the tidyverse (see function f.tidy below)

Can someone please post a solution for adding this column using mutate called from a inside function?
Jun 13, 2022 in Data Analytics by Avinash
• 1,260 points
515 views

1 answer to this question.

0 votes
We could translate it to a symbol using Rlang and then evaluate it using!!

function(df, old.col, new.col), f.tidy

percent > df percent

(new.col):= mutate(!!

(old.col + 1) rlang::sym

}

df = df, old.col = old.col, and new.col = new.col in f.tidy

Test Tcy Dan at #

#1 1 4 5

#2 2 5 6

#3 3 6 7

Or another option is mutate_at with rename_at

f.tidy <- function(df, old.col, new.col) {

df %>%

mutate_at(vars(old.col), funs(new = .+ 1)) %>%

rename_at(vars(matches("new")), ~ new.col)

}

f.tidy(df = df, old.col = old.col, new.col = new.col)

# test tcy dan

#1 1 4 5

#2 2 5 6

#3 3 6 7
answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

Assigning global variables from inside a function - R

You can assign global variables from inside ...READ MORE

answered May 15, 2018 in Data Analytics by Bharani
• 4,660 points
10,994 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,937 views
0 votes
1 answer
0 votes
1 answer

How to apply list to a function which give data frame as output

If you use  tidyverse, you can use ...READ MORE

answered Apr 11, 2018 in Data Analytics by Sahiti
• 6,370 points
794 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,030 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,180 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,890 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,113 views
0 votes
1 answer

How to use the strsplit function with a period

Since the dot has a specific meaning ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
797 views
0 votes
1 answer

How to rename a single column in a data.frame?

data.rename(columns={'gdp':'log(gdp)'}, inplace=True) The rename show that it accepts a dict ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
408 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP