To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.
The second way to select a column from a dataframe is to use the pipe operator %>% available as part of tidyverse. Here we first specify the name of the dataframe we want to work with and use the pipe %>% operator followed by select function with the column name we want to select.
I'm currently using R and came across the function all_of
in the tidyverse. What does this function exists for? It seems like I can use just x
at every point where all_of(x)
can be used..
Example:
library(tidyverse)
tb <- tibble(a=1:3, b=1:3, c=1:3)
x <- c("a", "b")
tb %>% select(all_of(x))
tb %>% select(x)
tb %>$ select(-all_of(x))
tb %>% select(-x)
The two lines with all_of
yield the same return values as the ones without the extra function. Why should I bother using them?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With