【问题标题】:R - Trouble with space and '/' in variable and column nameR - 变量和列名中的空格和“/”问题
【发布时间】:2017-03-16 18:23:35
【问题描述】:

在我的 Excel 文档中,我使用的列是“通信模式”,该列中的条目使用空格“亲自”或斜杠“IM/Chat”。

这些不断抛出错误:

  • eval 中的错误(expr、envir、enclos):找不到对象“In.Person”
  • IM/聊天也是如此

我尝试使用 In.Person 或 IM.Chat,但它们会引发错误。代码是:

png(filename="Number of Interactions by Communication Mode.png")
person <- subset(desktracker, Mode.of.Communication="")
ByChat <- ggplot(subset(desktracker, IM.Chat!=""), aes(x=IM.Chat)) +
  geom_bar() +
  theme_bw() +
  ylab("Number of Interactions") +
  xlab("By Chat") +
  theme(axis.title.y = element_text(color = "red"))
ByPerson <- ggplot(subset(person, In.Person =""), aes(x=In.Person)) +
  geom_bar() +
  theme_bw()+
  ylab("Number of Interactions") +
  xlab("In Person")
grid.arrange(ByChat, ByPerson, ByPhone, main="Number of Interactions by     Communication Mode
             for Reference 2016")
dev.off()

我是 R 新手,在有人离开后接手了这个项目。到目前为止,我已经尝试将我在 R 中编写“In Person”或“IM/Chat”的方式更改为不同程度的“In.Person”或“IM.Chat”,但没有成功。也许有一个通配符可以用于 IM/Chat 来克服“/”?我尝试将它在 excel 表中的显示方式更改为“In_Person”和“Chat”,这导致了同样的错误。我会继续胡闹,但欢迎任何建议。如果我找到解决方案,我一定会发布解决方案。

谢谢

不好意思,desktracker是我用来打开数据集的变量:

setwd("Z:/DesktrackR/Ref_Semester")
file = "Desk_Tracker_flat_8-17-15_through_5-9-16.csv"
desktracker <- read.csv(file, sep=",")

excel内容的一个想法:

【问题讨论】:

  • 至少对desktracker的内容一无所知,这是不可能回答的。请记住,如果您想避免这些问题,数据框中的变量名称不应包含空格或点以外的特殊字符。
  • 在语法上无效的变量名,例如带有空格或特殊符号的变量名,可以通过反引号`.在ggplot2中使用

标签: r excel ggplot2 rstudio


【解决方案1】:

我看不到您的变量名称,但我建议如果您确实有标题,请更正数据框的列名:

colnames(desktracker) <- gsub(" |/", '.', colnames(desktracker))

这会将空格或 / 替换为句点,因此您可以更轻松地访问变量。

注意:确保read.csv() 中有headers = TRUE,如:

setwd("Z:/DesktrackR/Ref_Semester")
file = "Desk_Tracker_flat_8-17-15_through_5-9-16.csv"
desktracker <- read.csv(file, sep=",", headers = TRUE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2015-11-11
    相关资源
    最近更新 更多