【发布时间】:2017-10-30 23:15:00
【问题描述】:
我有这个data.frame
set.seed(28100)
label_1 <- sample(c('first_col','second_col'), 10, replace = T)
dat <- data.frame(label_1,
value_1 = sample(1:100, 10, replace = T),
label_2 = sapply(label_1, FUN = function(x) ifelse(x == 'first_col', 'second_col', 'first_col')),
value_2 = sample(1:100, 10, replace = T))
head(dat)
label_1 value_1 label_2 value_2
1 first_col 88 second_col 84
2 first_col 40 second_col 30
3 first_col 98 second_col 32
4 second_col 80 first_col 64
5 first_col 34 second_col 43
6 second_col 52 first_col 10
两对键/值列的顺序不一致。我想把同样的数据reshape成一个长格式的data.frame,比如:
desired_dat <- data.frame(first_col = rep(NA, 10),
second_col = rep(NA, 10))
是否建议使用reshape2 或tidyr 来解决这个问题?具体如何?
【问题讨论】: