我正在尝试使用tidyr:spread()
.
id <- c(0,1,2,3,4,5,6,7,8,9)
key1 <- c("a", "a", "b", "b", "c","a", "a", "b", "b", "c")
val1 <- c(1,2,3,1,2,3,1,2,3,1)
key2 <- c("d",NA,NA,NA,"e","d","d",NA,"b",NA)
val2 <- c(1,NA,NA,NA,2,3,NA,NA,3,NA)
key3 <- c("x",NA,NA,NA,"e","d",NA,NA,NA,NA)
val3 <- c(0,NA,NA,NA,NA,3,1,NA,NA,NA)
df = data.frame(id, key1, val1, key2, val2, key3, val3)
library(tidyr)
c1 <- spread(df, key1, val1, fill = 0, convert = FALSE)
c2 <- spread(c1, key2, val2, fill = 0, convert = FALSE)
c3 <- spread(c2, key3, val3, fill = 0, convert = FALSE)
在运行 spread() 时,我收到以下错误:
[.data.frame(data, setdiff(names(data), c(key_col, value_col))) 中的错误:选择了未定义的列
这让我觉得问题出在值上,而不是在变量名中,正如错误所暗示的那样,有什么想法要寻找吗?
同样,是否有更有效的语法方法来传播多对键值变量?