【发布时间】:2017-04-06 10:41:00
【问题描述】:
我需要在数据框的所有列上gather_,除了一个。示例:
# I want to generate a dataframe whose column names are the letters of the alphabet. If you know of a simpler way, let me know!
foo <- as.data.frame(matrix(runif(100), 10, 10))
colnames(foo) <- letters[1:10]
现在,假设我想收集除e 列之外的所有列。这行不通:
mycol <- "e"
foo_melt <- gather_(foo, key = "variable", value = "value", -mycol)
#Error in -mycol : invalid argument to unary operator
这将:
column_list <- colnames(foo)
column_list <- column_list[column_list != mycol]
foo_melt <- gather_(foo, key = "variable", value = "value", column_list)
如果你问我,看起来很复杂。没有更简单的方法吗?
【问题讨论】:
-
一个选项是
setdiff,即gather_(foo, key = "variable", value = "value", setdiff(names(foo), mycol))