Search code examples
rselectdplyrpipeline

dplyr select column when column name is number


I want to reshape the data and then select a specific column.

data(ChickWeight)
chick <- ChickWeight %>% spread(Time,weight) %>% filter(Diet=="1")

It creates the column names for me, which are numbers. So how could I select the column that named "0"? I know that %>% select(3) may work, but I need the solution to select columns with their names being number.


Solution

  • Use backticks to select columns with their names being number

    data(ChickWeight)
    library(dplyr)
    library(tidyr)
    chick <- ChickWeight %>% spread(Time,weight) %>% filter(Diet==2) %>% select(`0`)