我需要在数据框的所有列上进行`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))
。 - akrun