3

Sometimes when generating a data frame from a list, the variable is named "." by default. How can I refer to this variable within dplyr functions, if only to change the variable name to something more appropriate.

# Code that produces my data frame with "." as column name
library(tidyverse)

d <- data.frame(`.` = 1, row.names = "a") 

# Now my code fails because `.` is a poor column name for dplyr functions:
d %>% select(model = rownames(.), outlier = `.`)
Joe
  • 3,217
  • 3
  • 21
  • 37
  • 2
    change the variable name... – B Williams Aug 31 '17 at 01:10
  • If you're open to changing the column names as B Williams suggested above, this post includes various approaches: https://stackoverflow.com/questions/6081439/changing-column-names-of-a-data-frame – Z.Lin Aug 31 '17 at 01:43
  • `.` will cause more problems than just with dplyr. Change the variable name. – Hong Ooi Aug 31 '17 at 01:49
  • Hello @HongOoi, I agree, this is just a reprex. The output from another function is naming the column ".". – Joe Sep 01 '17 at 04:52

2 Answers2

1

This isn't actually a problem with the column named . its a problem with referencing the rownames in select() see

d <- data.frame(test = 1, row.names = "a")
d %>% select(model = rownames(.), outlier = test)

still returns Error: Strings must match column names. Unknown columns: a

just use

d <- data.frame(`.` = 1, row.names = "a") 
d %>% select(outlier = '.')

will rename the column to outlier

Sarah
  • 3,022
  • 1
  • 19
  • 40
0

Given

d <- data.frame(`.` = 1, row.names = "a") 

Base R Solution

colnames(d) <- 'newname'

Dplyr Solution

d %>% rename(newname = '.')
Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69