【发布时间】:2020-08-26 08:58:12
【问题描述】:
背景
我有一个数据集,df。每当我尝试重命名“TanishaIsCool”列时,都会收到错误消息:意外字符串常量。我希望在我的列名中添加空格
TanishaIsCool Hello
hi hi
这就是我正在做的:
df1 <- df %>% rename(Tanisha Is Cool = `TanishaIsCool` )
期望的输出
Tanisha Is Cool Hello
hi hi
投入
structure(list(TanishaIsCool = structure(1L, .Label = "hi", class = "factor"),
Hello = structure(1L, .Label = "hi", class = "factor")), class = "data.frame", row.names = c(NA,
-1L))
【问题讨论】:
-
您需要将 ` 反引号放在包含空格的无效名称周围 -
df1 <- df %>% rename(`Tanisha Is Cool` = TanishaIsCool)。否则,R 解释器会将Tanishais和Cool视为单独的语句,只是浮动在那里 - 因此有点神秘的“意外字符串常量” -
谢谢! thelatemail - 这也有效!