passing function argument to dplyr select

0 votes
I can do this to pick out a few columns from a dataframe.

require(dplyr)

require(magrittr)

df - data.frame (col1 = c(1, 2, 3), col2 = letters[1:3], col3 = LETTERS[4:6])

percent > df percent

select (col1, col2)

I'd like to create a function that works like

F is a function (data, firstCol, secondCol)

{

information percent > percent

substitute (firstCol, secondCol) in the choose box

}

But when I run f(df, col1, col2), I get an error.

Select vars(names(.data),..., env = parent.frame()) has encountered an error:

It is impossible to force a (list) object to type "double."

originating from: (function ()

{

.rs.breakOnError(TRUE)

})()

EDIT -- slightly less trivial example:

Suppose I wanted to do

mtcars %>%

select(cyl, hp) %>%

unique %>%

group_by(cyl) %>%

summarise(avgHP = mean(hp))

but with different datasets and different variable names. I could reuse the code and replace mtcars, cyl, and hp. But I'd rather wrap it all up in a function
Jul 9, 2022 in Data Science by avinash
• 1,840 points
557 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Data Science

0 votes
1 answer

How to implement Knn-algorithm without using k-nn function in r?

I created an example that demonstrates the ...READ MORE

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
465 views
0 votes
0 answers

To speed up the tapply function in R, or another function to convert data frame into a matrix

I must turn a sizable dataset into ...READ MORE

Jun 24, 2022 in Data Science by Sohail
• 3,040 points
360 views
0 votes
0 answers

Why 'nrow()' function doesn't seem to work within 'summarise()' function?

See the code below: library(dslabs) \sdata(gapminder) gapminder percent > ...READ MORE

Jun 30, 2022 in Data Science by avinash
• 1,840 points
337 views
0 votes
0 answers

In R, how to get an object's name after it is sent to a function?

I'm trying to find the opposite of ...READ MORE

Jul 5, 2022 in Data Science by avinash
• 1,840 points
300 views
0 votes
1 answer

Converting xlsx to xls using Microsoft Office Compitablity Pack's excelcnv

Simply rearranging the positions of the parameters ...READ MORE

answered Mar 25, 2022 in Data Science by gaurav
• 23,260 points
1,283 views
0 votes
0 answers

Introduction to Statistical Learning with Applications in R Figure Codes

I recently bought the following book: An Introduction ...READ MORE

Jun 1, 2022 in Data Science by avinash
• 1,840 points
566 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,035 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,185 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,892 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,116 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