-1

data$name*

I have a data file with many columns and i know how they begin and want to get them one by one just using the initial part of the name and using the wild card. but it doesn't seem to work with the $ sign. it expects something more

+

Can anyone explain this?

Virgo
  • 11
  • 1
  • 2
  • 1
    you can try `data[,grep("name",names(data))]` – mtoto Jul 13 '16 at 11:26
  • this works, thanks but can you help me understand also why data$name* does not work? – Virgo Jul 13 '16 at 11:34
  • Why would you think that would work in `R` ? – mtoto Jul 13 '16 at 11:39
  • I'm just ignorant, trying to understand if there are specific rules for when wildcard works and when it doesn't… I don't see the difference between having "name*" inside grep and having $name*... – Virgo Jul 13 '16 at 11:49
  • i did find and read that post thread… but could not see an answer to my $ question… thanks – Virgo Jul 13 '16 at 12:01

1 Answers1

0

See the example below:

df <- data.frame(name = c(1, 2, 3), name1 = c(1, 1, 2), colname = c(1, 1, 1)) 
col_sel <- grep("^name", colnames(df), value = TRUE)
df[col_sel]

^ ensures that the name of the column begins with 'name'.

streof
  • 145
  • 3
  • 8