我有一个长格式的数据集,并希望使用 Reshape 或 Reshape 之前的任何预处理将其转换为宽格式。困难在于“值”变量是非数字的。请注意,原始数据中也有合法的重复记录。以下代码显示了每个的数据布局。
id = c(1, 1, 1, 1, 1, 1, 1)
month <- c("jan", "feb", "feb", "march", "april", "april", "april")
stress <- c("mild", "mild", "high", "moderate", "mild", "high", "mild")
Longdata <- data.frame(id, month, stress, stringsAsFactors = FALSE)
这是原始格式:
> Longdata
id month stress
1 1 jan mild
2 1 feb mild
3 1 feb high
4 1 march moderate
5 1 april mild
6 1 april high
7 1 april mild
这就是我想要组织数据的方式:
id <- c(1)
jan <- c("mild")
feb <- c("mild-high")
march <- c("moderate")
april <- c("mild-high-mild")
widedata <- data.frame(id, jan, feb, march, april, stringsAsFactors = FALSE)
> widedata
id jan feb march april
1 1 mild mild-high moderate mild-high-mild