如何在 R 中将数据从长格式重新排列为宽格式?
可以使用 reshape 函数来完成此操作。
示例
> data <- data.frame( name = rep(c("firstName", "LastName"), each=5), salarygroup = rep(1:5, 2), Errors = sample(21:60,10,replace=TRUE) ) > data
name salarygroup Errors
1 firstName 1 58 2 firstName 2 50 3 firstName 3 47 4 firstName 4 29 5 firstName 5 36 6 LastName 1 34 7 LastName 2 40 8 LastName 3 54 9 LastName 4 38 10 LastName 5 41 > reshape(data, idvar = "name", timevar = "salarygroup", direction = "wide")
name Errors.1 Errors.2 Errors.3 Errors.4 Errors.5
1 firstName 58 50 47 29 36 6 LastName 34 40 54 38 41
广告