0
使用reshape功能:
reshape(dat1, idvar = "name", timevar = "numbers", direction = "wide")
收藏
set.seed(45)
dat1 <- data.frame(
name = rep(c("firstName", "secondName"), each=4),
numbers = rep(1:4, 2),
value = rnorm(8)
)
dat1
name numbers value
1 firstName 1 0.3407997
2 firstName 2 -0.7033403
3 firstName 3 -0.3795377
4 firstName 4 -0.7460474
5 secondName 1 -0.8981073
6 secondName 2 -0.3347941
7 secondName 3 -0.5013782
8 secondName 4 -0.1745357
我想重塑它,以便每个唯一的“名称”变量都是一个行名,其中“值”是该行的观察值,“数字”是同名字。有点像这样:
name 1 2 3 4
1 firstName 0.3407997 -0.7033403 -0.3795377 -0.7460474
5 secondName -0.8981073 -0.3347941 -0.5013782 -0.1745357
我已经看过了melt,cast还有其他一些东西,但似乎都没有。
使用reshape功能:
reshape(dat1, idvar = "name", timevar = "numbers", direction = "wide")