Joe
Joe

Reputation: 3806

Using dplyr functions on variables named "."

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 = `.`)

Upvotes: 3

Views: 967

Answers (2)

Rich Pauloo
Rich Pauloo

Reputation: 8412

Given

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

Base R Solution

colnames(d) <- 'newname'

Dplyr Solution

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

Upvotes: 0

Sarah
Sarah

Reputation: 3519

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

Upvotes: 1

Related Questions