Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
由此:
> test <- data.frame(x = c("a","a","a"), y = c("b","b","c"), z = c(1,2,1)) > test x y z 1 a b 1 2 a b 2 3 a c 1
对此:
x b c 1 a 1 NA 2 a 2 NA 3 a NA 1
由于数据框中的x列test不唯一标识行,但您不想进行任何聚合,您需要使用唯一id列扩充数据框,然后dcast()从reshape2包中使用:
x
test
id
dcast()
reshape2
require(reshape2) test$id <- 1:nrow(test) > dcast(test, id + x ~ y, value_var = 'z')[,-1] x b c 1 a 1 NA 2 a 2 NA 3 a NA 1