此问题已在此处有答案:
How to reshape data from long to wide format(14个答案)
7天前关闭
我有一个这样的数据框
test <- data.frame(matrix(nrow = 18, ncol = 3))
colnames(test) <- c("subject","session","f1")
test$subject <- c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3)
test$session <- c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
test$f1 <- c(29,52,72,42,50,52,30,49,63,51,37,43,1,3,2,1,2,0)
我希望它看起来像这样
test <- data.frame(matrix(nrow=6,ncol = 5))
colnames(test) <- c("subject","session","t1","t2","t3")
test$subject <- c(1,2,3,1,2,3)
test$session <- c(1,1,1,2,2,2)
test$t1 <- c(29,42,30,51,1,1)
test$t2 <- c(52,50,49,37,3,2)
test$t3 <- c(72,52,63,43,2,0)
我该怎么去改变它?
1条答案
按热度按时间5ktev3wc1#
我们可以只使用
pivot_wider()
和names_prefix()
参数:正如@Martin Gal在没有unnest
的情况下提出的: