如何使用 R 从 data.frame 创建一个 table,其中一个单元格可以有多个值
How to create a table from a data.frame where a cell could have multiple values using R
我曾尝试查看备忘单并查看此处提出的其他问题,但未能找到答案。
我正在使用 R,我的 data.frame 看起来像这样:
我想将第二列设为垂直类别,将第三列设为水平类别。然后第一列将与其行中的相应类别匹配。
这是我想如何格式化 table 的示例:
有没有办法编写代码来执行此操作,以避免使用 Excel 和 Word 创建 table?
试试这个:
df = data.frame(let = LETTERS[1:12],
vert = c(10, 10, 2.5, 5, 10, 5, 2.5, 10, 1.25, 1.25, 1.25, 1.25),
hor = c(2,2,3,2,4,2,3,4,1,4,4,1),
stringsAsFactors = F)
# find unique combinations
positions = expand.grid(unique(df$vert), unique(df$hor))
# pre-allocate matrix
M = matrix(ncol = length(unique(df$hor)),
nrow = length(unique(df$vert)))
rownames(M) <- sort(unique(df$vert))
colnames(M) <- sort(unique(df$hor))
# loop over valid positions and put them in the matrix
for (i in c(1:nrow(positions))){
# get row
row = as.numeric(positions[i,])
# gather all entries that go in position
valid = df[df$vert == row[1] & df$hor == row[2], 'let']
valid = paste(valid, collapse=",")
# get matrix indices
vert_i <- which(rownames(M) == row[1])
horiz_i <- which(colnames(M) == row[2])
# put the data in the matrix
M[vert_i, horiz_i] <- valid
}
print(M)
它可以更有效率,但它完成了工作。
这是一种方法:
library(tidyr)
library(dplyr)
# example data
exd <- data.frame(
Vertical = c(10,10,2.5,5,10,5,2.5,10,1.25,1.25,1.25, 1.25),
Horizontal = c(2,2,3,2,4,2,3,4,1,4,4,1),
row.names = LETTERS[1:12])
# move values into a column
exd <- mutate(exd,
Value = rownames(exd))
# aggregate by Vertical and Horizontal
exd <- summarize(group_by(exd, Vertical, Horizontal),
Value = paste(Value, collapse = ","))
# re-arrange into the desired form
spread(exd, Horizontal, Value)
我曾尝试查看备忘单并查看此处提出的其他问题,但未能找到答案。
我正在使用 R,我的 data.frame 看起来像这样:
我想将第二列设为垂直类别,将第三列设为水平类别。然后第一列将与其行中的相应类别匹配。
这是我想如何格式化 table 的示例:
有没有办法编写代码来执行此操作,以避免使用 Excel 和 Word 创建 table?
试试这个:
df = data.frame(let = LETTERS[1:12],
vert = c(10, 10, 2.5, 5, 10, 5, 2.5, 10, 1.25, 1.25, 1.25, 1.25),
hor = c(2,2,3,2,4,2,3,4,1,4,4,1),
stringsAsFactors = F)
# find unique combinations
positions = expand.grid(unique(df$vert), unique(df$hor))
# pre-allocate matrix
M = matrix(ncol = length(unique(df$hor)),
nrow = length(unique(df$vert)))
rownames(M) <- sort(unique(df$vert))
colnames(M) <- sort(unique(df$hor))
# loop over valid positions and put them in the matrix
for (i in c(1:nrow(positions))){
# get row
row = as.numeric(positions[i,])
# gather all entries that go in position
valid = df[df$vert == row[1] & df$hor == row[2], 'let']
valid = paste(valid, collapse=",")
# get matrix indices
vert_i <- which(rownames(M) == row[1])
horiz_i <- which(colnames(M) == row[2])
# put the data in the matrix
M[vert_i, horiz_i] <- valid
}
print(M)
它可以更有效率,但它完成了工作。
这是一种方法:
library(tidyr)
library(dplyr)
# example data
exd <- data.frame(
Vertical = c(10,10,2.5,5,10,5,2.5,10,1.25,1.25,1.25, 1.25),
Horizontal = c(2,2,3,2,4,2,3,4,1,4,4,1),
row.names = LETTERS[1:12])
# move values into a column
exd <- mutate(exd,
Value = rownames(exd))
# aggregate by Vertical and Horizontal
exd <- summarize(group_by(exd, Vertical, Horizontal),
Value = paste(Value, collapse = ","))
# re-arrange into the desired form
spread(exd, Horizontal, Value)