Unexpected Symbol Error in my code

I am running this line of code in Rstudio and getting an Unexpected Symbol Error.

trimmed_flavors_df <- flavors_df %>% select(Rating, Cocoa Percent, Company Location)

The spaces in Cocoa Percent and Company Location probably...
Try select(Rating, all_of(c("Cocoa Percent", "Company Location"))), although I'm not sure whether that'll work. Renaming the variables to remove the spaces would probably be better in the long run though

The way to reference non-syntactic variable names in R is among backticks not quotes (e. g. `Company Location` not "Company Location")

all_of works just fine too. back ticks does result in shorter code though...

> dat <- data.frame("foo bar" = 1:5, x = letters[1:5]) 
> names(dat)[1] <- "foo bar"
> select(dat, all_of("foo bar"))
  foo bar
1       1
2       2
3       3
4       4
5       5
> select(dat, `foo bar`)
  foo bar
1       1
2       2
3       3
4       4
5       5

all_of has the advantage that you can pass string vectors in though, so there are places to use it...

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.