如何将多列数据帧转换为两列数据帧。
例如,我有一个包含四列的数据集
foo<-data.frame(
w= rnorm(10, mean=50, sd=10),
x= sample(1:10, 10, replace=T),
y=c("red", "red", "red", "blue", "blue",
"blue", "yellow", "yellow", "yellow",
"green"),
z=runif(10, 5.0, 7.5))
w x y z
42.67991 8 red 7.460069
46.40567 5 red 6.159747
65.67709 8 red 7.352203
57.53091 4 blue 5.838025
37.31885 1 blue 5.486540
46.38344 5 blue 5.848174
50.65255 1 yellow 7.434667
45.31212 1 yellow 7.449169
49.10322 10 yellow 6.611908
39.10083 10 green 6.539835
我试图将这四列数据帧转换为两列数据帧(a,b),如下所示
a b
42.67991 8
46.40567 5
. .
. .
red 7.460069
red 6.159747
. .
. .
8 red
5 red
. .
. .
42.67991 red
46.40567 red
. .
. .
42.67991 7.460069
46.40567 6.159747
. .
. .
8 7.460069
5 6.159747
非常感谢任何帮助。
答案 0 :(得分:0)
尝试
indx <- combn(1:ncol(foo), 2)
res <- do.call(rbind,lapply(split(indx, col(indx)), function(i) {
x1 <- foo[i]
colnames(x1) <- letters[1:2]
x1}))
row.names(res) <- NULL
head(res)
# a b
#1 42.67991 8
#2 46.40567 5
#3 65.67709 8
#4 57.53091 4
#5 37.31885 1
#6 46.38344 5
或者
res1 <- do.call(rbind,combn(1:ncol(foo), 2, FUN= function(i)
list(setNames(foo[i], letters[1:2]))))
foo <- structure(list(w = c(42.67991, 46.40567, 65.67709, 57.53091,
37.31885, 46.38344, 50.65255, 45.31212, 49.10322, 39.10083),
x = c(8L, 5L, 8L, 4L, 1L, 5L, 1L, 1L, 10L, 10L), y = c("red",
"red", "red", "blue", "blue", "blue", "yellow", "yellow",
"yellow", "green"), z = c(7.460069, 6.159747, 7.352203, 5.838025,
5.48654, 5.848174, 7.434667, 7.449169, 6.611908, 6.539835
)), .Names = c("w", "x", "y", "z"), class = "data.frame",
row.names = c(NA, -10L))