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 = `.`)
4
  • 2
    change the variable name... Commented Aug 31, 2017 at 1:10
  • If you're open to changing the column names as B Williams suggested above, this post includes various approaches: stackoverflow.com/questions/6081439/… Commented Aug 31, 2017 at 1:43
  • . will cause more problems than just with dplyr. Change the variable name. Commented Aug 31, 2017 at 1:49
  • Hello @HongOoi, I agree, this is just a reprex. The output from another function is naming the column ".". Commented Sep 1, 2017 at 4:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Given

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

Base R Solution

colnames(d) <- 'newname'

Dplyr Solution

d %>% rename(newname = '.')

4 Comments

With the dplyr solution, you can rename and pipe onwards!
setNames/purrr::set_names pipes nicely
When I use your dplyr solution I get "Error in captureDots(strict = __quosured) : the argument has already been evaluated"
@Joe, you're right. I forgot to include quote marks, so I updated the answer. That dplyr solution should work now. Try it out. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.