我需要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,鍵= 「可變」,值= 「值」,setdiff(名稱(富),mycol))' – akrun