- 此问题在此处已有答案**:
How to reshape data from long to wide format(14个答案)
Order columns in R based on numeric sequence(1个答案)
2天前关闭。
我有一个 Dataframe ,看起来像这样:
df <- data.frame(ID = c("A","A","A","A","A","B","B","B","B","B","C","C","C","C","C"),
code = c(1,2,2,3,3,1,2,2,1,1,3,2,2,1,1),
class = c(2,4,5,5,2,3,2,5,1,2,4,5,3,2,1),
n = c(10,10,20,15,25,20,10,10,20,25,10,15,10,15,15))
我想把它转置一下,这样class
列就变成了行,n
就基于ID
,class
和n
展开,看起来就像这样:
|ID|class|1 |2 |3 |4 |5 |
|A |1 | |10| | | |
|A |2 | | | |10|20|
|A |3 | |25| | |15|
....
1条答案
按热度按时间whitzsjs1#
您可以使用
pivot_wider
和arrange
来获得列的数字顺序,如下所示:创建于2022年12月27日,使用reprex v2.0.2