首页 > 解决方案 > 如何使用 check.names = false 但仍使用运算符 $

问题描述

我有包含括号和空格的列名。我可以使用check.names = False.

但是,导入子集功能后不再起作用。我尝试了以下代码

test <- subset (df$"model code" == graph)
test <- subset (df, "model code" == graph)
test <- subset (df$'model code' == graph)

这是输出错误

subset.default(df$ model mode== "graph") 中的错误:缺少参数 "subset",没有默认值

我如何使用 check.names 为假但仍使用运算符 $

这可行,但是使用以下代码在下游发生了一个新错误FUN(X[[i]], ...) 中的错误:对象'G'没有找到有关如何解决此问题的任何想法?

p2 <- ggplot(df, aes_string("Sample",i)) + 
    geom_boxplot(show.legend = F) + 
    geom_beeswarm(aes(color = Sample), size=2)

标签: rimport

解决方案


您可以像这样使用反引号,并且您可能需要在因子水平周围加上引号。

subset(dat, `model code` == "graph")
#   model code          x
# 5      graph  1.8951935
# 7      graph -0.2572694

如果需要$,最好使用括号进行子集化:

dat[dat$"model code" == "graph", ]
#   model code          x
# 5      graph  1.8951935
# 7      graph -0.2572694

推荐阅读