我有2个colomns,第一个是月份,第二个是类似的值
month windspeed
jan 2.5
jan 1.6
jan 4.5
feb 3.6
feb 3.5
feb 1.8
mar 4.2
mar 4.6
mar 3.8
我想重塑那种方式
jan feb mar
2.5 3.6 4.2
1.6 3.5 4.6
4.5 1.8 3.8
需要帮助!
答案 0 :(得分:3)
您可以使用unstack
base R
unstack(df1, windspeed~month)
# jan feb mar
#1 2.5 3.6 4.2
#2 1.6 3.5 4.6
#3 4.5 1.8 3.8
或创建按“月”分组的序列列,然后使用dcast
library(reshape2)
df2 <- transform(df1, indx=ave(seq_along(month), month, FUN=seq_along))
dcast(df2, indx~month, value.var='windspeed')[-1]
# jan feb mar
#1 2.5 3.6 4.2
#2 1.6 3.5 4.6
#3 4.5 1.8 3.8