Skip to content

Instantly share code, notes, and snippets.

@ClintWeathers
Last active December 24, 2017 03:05
Show Gist options
  • Save ClintWeathers/0d7496cf1c42df5ad4e6 to your computer and use it in GitHub Desktop.
Save ClintWeathers/0d7496cf1c42df5ad4e6 to your computer and use it in GitHub Desktop.
Trying to make a new dataframe using Dplyr
### Trying to create new dataframes from dplyr 0.4.3 functions using R 3.2.2.
#Very Simple Example:
paste("cars_with", 6, "cylinders", sep = "_") <- filter(mtcars, cyl == 6)
#What I want there is a dataframe named "cars_with_6_cylinders" that has the results of this:
filter(mtcars, cyl == 6)
#But instead, what I get is this:
"Error in paste("cars_with", 6, "cylinders", sep = " ") <- filter(mtcars, :
target of assignment expands to non-language object"
#Ultimately, what I'd like is something more complex like:
paste("cars_with", 6, "cylinders", sep = "_") <- filter(mtcars, cyl == 6) %>%
group_by(gear) %>%
summarise(number_of_cars = n()))
#And wrap that up in a function like this:
gearframes <- function(x){
paste("cars_with", x, "cylinders", sep = "_") <- filter(mtcars, cyl == x) %>%
group_by(gear) %>%
summarise(number_of_cars = n()))
}
#Then I could feed the function something like:
sapply(mtcars$cyl, gearframes)
#And get dataframes for each numer of cylinders in mtcars$cyl.
@cperk
Copy link

cperk commented Dec 24, 2017

Did you ever figure this out? I am getting the same error when I resample my data with group_by() %>% sample_n() %>% bind_rows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment