When quoting names in dplyr use backticks, not single or double quotes

754 views
Skip to first unread message

Bob

unread,
Jun 19, 2016, 10:08:23 AM6/19/16
to manipulatr
Hi All, 

I'm not sure if this intentional, but if you have blanks in variable names, dplyr's select function won't read them. Quoting such names using backticks works fine. Example below.  As help(Quotes) says, " Almost always, other names can be used provided they are quoted. The preferred quote is the backtick (`), and deparse will normally use it, but under many circumstances single or double quotes can be used..."

Cheers,
Bob

 
> library("readr")
> df <- read_csv(
+   "id,blood pressure
+   1, 85
+   2, 120
+   3, 134")
> df
Source: local data frame [3 x 2]

     id blood pressure
  (int)          (int)
1     1             85
2     2            120
3     3            134
> # Any type of quote works with mean 
> mean(df$"blood pressure")
[1] 113
> mean(df$'blood pressure')
[1] 113
> mean(df$`blood pressure`)
[1] 113
> # And with summary
> summary(df$"blood pressure")
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   85.0   102.5   120.0   113.0   127.0   134.0 
> summary(df$'blood pressure')
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   85.0   102.5   120.0   113.0   127.0   134.0 
> summary(df$`blood pressure`)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   85.0   102.5   120.0   113.0   127.0   134.0 
> # But only backticks work with select
> library("dplyr")
> dplyr::select(df, "blood pressure")
Error: All select() inputs must resolve to integer column positions.
The following do not:
*  "blood pressure"
> dplyr::select(df, 'blood pressure')
Error: All select() inputs must resolve to integer column positions.
The following do not:
*  "blood pressure"
> dplyr::select(df, `blood pressure`)
Source: local data frame [3 x 1]

  blood pressure
           (int)
1             85
2            120
3            134


> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3 readr_0.2.2

loaded via a namespace (and not attached):
[1] lazyeval_0.1.10 magrittr_1.5    R6_2.1.2        assertthat_0.1  parallel_3.3.0 
[6] DBI_0.4-1       tools_3.3.0     Rcpp_0.12.5  
Reply all
Reply to author
Forward
0 new messages