[英]How to transform a dataframe into a matrix of combinations
I have a data frame of one row that contains the results of an equation of two variables (let's call them X and Y; for simplicity, the equation is a sum).我有一行的数据框,其中包含两个变量的方程的结果(我们称它们为 X 和 Y;为简单起见,方程是一个总和)。 I need to transform that dataframe into a matrix where I can see the combinations of those variables.
我需要将 dataframe 转换为一个矩阵,我可以在其中看到这些变量的组合。 This is a reproducible example:
这是一个可重现的例子:
x1_y1 <- 2 #Here x = 1, y = 1
x1_y2 <- 3 #Here x = 1, y = 2
x2_y1 <- 3 #Here x = 2, y = 1
x2_y2 <- 4 #Here x = 2, y = 2
df <- data.frame(x1_y1, x1_y2, x2_y1, x2_y2)
I would like the output to be something like this:我希望 output 是这样的:
Does anybody know how to achieve this?有谁知道如何实现这一目标? Thanks in advance!
提前致谢!
A tapply
attempt after extracting the name components via strcapture
:通过
strcapture
提取名称组件后的tapply
尝试:
tapply(unlist(df), strcapture("(.+)_(.+)", names(df), proto=list(x="",y="")), I)
# y
#x y1 y2
# x1 2 3
# x2 3 4
This works...这有效...
(Similar to thelatemail's answer, though ever-so-slightly different... and written at the same time:-) (类似于 thelatemail 的答案,虽然略有不同......同时写的:-)
tmp <- cbind(strcapture("(.+)_(.+)", names(df), list(x="", y="")), val = unlist(df, use.names = FALSE))
tmp <- reshape2::dcast(x ~ y, value.var = "val", data = tmp)
rownames(tmp) <- tmp[[1]]
tmp <- tmp[,-1]
as.matrix(tmp)
# y1 y2
# x1 2 3
# x2 3 4
Perhaps a bit contrived...可能有点做作...
library(dplyr)
library(tidyr)
tmp <- df %>%
pivot_longer(everything()) %>%
separate(name, sep = "_", into = c("x", "y")) %>%
pivot_wider(x, names_from = "y", values_from = "value") %>%
as.data.frame()
rownames(tmp) <- tmp[[1]]
tmp <- tmp[,-1]
as.matrix(tmp)
# y1 y2
# x1 2 3
# x2 3 4
A tidyr
solution:一个
tidyr
的解决方案:
library(tidyr)
df %>%
pivot_longer(everything(), names_to = c("x", ".value"), names_sep = "_") %>%
tibble::column_to_rownames("x")
# y1 y2
# x1 2 3
# x2 3 4
The output is still a data.frame. output 仍然是一个数据帧。 You could pass it to
as.matrix()
if necessary.如有必要,您可以将其传递给
as.matrix()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.