pivot_wider() function in R, and column names from a data frame

Hey everyone, beginner to R. I was working with the "Starwars" dataset and learning about the pivot_wider() function in R, and I noticed through a general YouTube video that the column names were put inside quotations. I thought that was weird because typically column names are not put in quotes since quotes are typically viewed as text in R (which is what I was told through this platform from a previous question). And, in fact, when I did not use the quotations around the column names, but result was the exact same and worked perfectly fine as it did with the quotations. So, my question is, are the quotations necessary? Won't it make more sense for me not to use the quotation since we are simply referring to column names and not "text"? Below is my syntax to explain it visually if you didn't quite understand what I was talking about. Thank you.

Syntax with quotations:

sw <- select(starwars, name, height) %>%
pivot_wider(names_from = "name",
values_from = "height")

A partial screenshot of my results:

Syntax without quotations:

sw <- select(starwars, name, height) %>%
pivot_wider(names_from = name,
values_from = height)

A partial screenshot of my results (as you can see, I get the same result as the syntax above):

This has to do with R versus "tidyverse", which is a bunch of packages added to R to allow users to do things in certain ways. Ordinarily (i.e., outside the tidyverse) if a function f takes an argument named x and I invoke it as f(x=y), R expects y to be a variable containing the value of the argument. So if pivot_wider were an ordinary R function, in your second version R would look for a variable named name containing in this case a string giving the name of a column.

The tidyverse packages include a number of functions that are specifically designed to let you specify a variable name from a database without the quotes. The tidyr package (the source of pivot_wider) is one of the tidyverse packages. So yes, it works without the quotes, but only because you are using a function specifically designed to allow that. Try that with a function not part of the tidyverse and you will encounter an error.

1 Like

Thank you for the response! To clarify: so basically when I learned that column names from a data frame do not need to be referenced with quotations, that is actually only true when working with the tidy verse package. When working with functions from base R, you DO need quotations to refer to a column, correct? At least that is what I'm getting through the response.

It is often true that in base R functions you need to quote data frame column names. However, the subset() function of base R is an exception and there may be others.

1 Like

Thank you for confirming!