user35131
user35131

Reputation: 1134

Identify column names in R that contain white spaces

I have a dataframe

df

Some columns in the dataframe

column1|column2|column3|`column 4`|column5|`column 6`

How could I identify column names containing spaces (column 4 and column 6) and export them as vectors?

c(`column 4`,`column 6`)

Upvotes: 1

Views: 307

Answers (2)

Baraliuh
Baraliuh

Reputation: 593

This should work:

  df %>% 
      dplyr::select(contains(' ')) %>%
      names()

Upvotes: 2

TheSciGuy
TheSciGuy

Reputation: 1196

I believe you want to use select from dplyr:

df %>%
    select(contains("X"))

vec4 <- df$X.column.4.
vec6 <- df$X.column.6.
vec46 <- c(vec4, vec6)

Upvotes: 1

Related Questions