我在R中有一个数据框,看起来像这样:
df <- paste0(c(letters[seq( from = 1, to = 5 )]),":",round(runif(5),2))
df <- as.data.frame(t(df))
df2 <- paste0(c(letters[seq( from = 1, to = 5 )]),":",round(runif(5),2))
df2 <- as.data.frame(t(df2))
df3 <- paste0(c(letters[seq( from = 1, to = 5 )]),":",round(runif(5),2))
df3 <- as.data.frame(t(df3))
df <- rbind(df, setNames(sample(df2), names(df2)))
df <- rbind(df, setNames(sample(df3), names(df3)))
df
V1 V2 V3 V4 V5
1 a:0.21 b:0.49 c:0.96 d:0.95 e:0.03
2 b:0.33 c:0.25 e:0.81 d:0.94 a:0.02
3 c:0.34 e:0.26 a:0.31 d:0.55 b:0.63
冒号前的字符反映了测量的类型,冒号后的数字反映了测量本身。
我想重新排列每一行,以便每种测量(即,冒号前的字符)在同一列中。 应该看起来像这样:
V1 V2 V3 V4 V5
1 a:0.21 b:0.49 c:0.96 d:0.95 e:0.03
2 a:0.02 b:0.33 c:0.25 d:0.94 e:0.81
3 a:0.31 b:0.63 c:0.34 d:0.55 e:0.26
或者更好的方法是:
a b c d e
1 0.21 0.49 0.96 0.95 0.03
2 0.02 0.33 0.25 0.94 0.81
3 0.31 0.63 0.34 0.55 0.26
有什么方法可以重新排列每行,使得在给定列中冒号前的字符相同?
非常感谢您提供的建议!