【发布时间】:2016-04-13 16:14:13
【问题描述】:
我有一个非常宽的 df(85 列),我想使用 gather 将其转换为长格式。我没有使用-c(all the columns I do not want to gather) 语法来保留列,而是创建了列名的对象并得到了错误。
Error in -c(KeepThese) : invalid argument to unary operator
例如,使用带有一些附加字段的iris
require(tidyr)
iris$Season <- sample(c("AAA", "BBB"), nrow(iris), replace = T)
iris$Var <- sample(c("CCC", "DDD"), nrow(iris), replace = T)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Season Var
1 5.1 3.5 1.4 0.2 setosa AAA DDD
2 4.9 3.0 1.4 0.2 setosa AAA CCC
3 4.7 3.2 1.3 0.2 setosa BBB CCC
4 4.6 3.1 1.5 0.2 setosa BBB CCC
5 5.0 3.6 1.4 0.2 setosa BBB DDD
6 5.4 3.9 1.7 0.4 setosa AAA DDD
我想收集除 5:7 之外的所有列,它们被制成下面的一个对象。
KeepThese <- colnames(iris)[5:7]
现在,我想gather 除 5:7 之外的所有列,并调用 ID 列 Part 和数字字段 Value,并使用以下代码并得到错误。
dat <- iris %>% gather(Part, Value, -c(KeepThese))
Error in -c(KeepNames) : invalid argument to unary operator
如何指定一堆我不想收集的列而不在tidyr 中写出每一列?
添加为什么我的代码不起作用?
【问题讨论】: