有时从列表生成数据框时,变量名为“。”。默认情况下。如果只是将变量名更改为更合适的名称,我如何在dplyr函数中引用此变量。
# 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 = `.`)
答案 0 :(得分:1)
这实际上并不是名为列的问题。引用select()中的rownames是一个问题,请参阅
d <- data.frame(test = 1, row.names = "a")
d %>% select(model = rownames(.), outlier = test)
仍会返回Error: Strings must match column names. Unknown columns: a
只需使用
d <- data.frame(`.` = 1, row.names = "a")
d %>% select(outlier = '.')
会将列重命名为异常值
答案 1 :(得分:0)
<强>鉴于强>
d <- data.frame(`.` = 1, row.names = "a")
基础R解决方案
colnames(d) <- 'newname'
Dplyr解决方案
d %>% rename(newname = '.')