假设我有一张这样的桌子:
data <- c(1,2,3,6,5,6,9,"LC","LC","HC","HC","LC","HC","ALL")
attr(data,"dim") <- c(7,2)
data
[,1] [,2]
[1,] "1" "LC"
[2,] "2" "LC"
[3,] "3" "HC"
[4,] "6" "HC"
[5,] "5" "LC"
[6,] "6" "HC"
[7,] "9" "ALL"
现在我想操纵数据,看起来像这样:
[,"LC"] [,"HC"] [,"ALL"]
[1,] "1" "3" "9"
[2,] "2" "6"
[3,] "5" "6"
有没有办法在 R 中执行此操作,或者这是不可能的,我应该尝试其他方式来访问我的数据吗?
答案 0 :(得分:3)
使用split
可以非常接近。这将返回包含所需值的列表,然后您可以使用lapply
或任何其他列表操作函数:
split(data[, 1], data[, 2])
$ALL
[1] "9"
$HC
[1] "3" "6" "6"
$LC
[1] "1" "2" "5"
如果你必须以矩阵格式输出,那么我建议你用NA填充短矢量:
x <- split(data[, 1], data[, 2])
n <- max(sapply(x, length))
pad_with_na <- function(x, n, padding=NA){
c(x, rep(padding, n-length(x)))
}
sapply(x, pad_with_na, n)
这导致:
ALL HC LC
[1,] "9" "3" "1"
[2,] NA "6" "2"
[3,] NA "6" "5"
答案 1 :(得分:0)
示例数据
我更喜欢将数据读入data.frame,因为它检查向量是否长度相等。
data <- data.frame(X=c(1,2,3,6,5,6,9),
Y=c("LC","LC","HC","HC","LC","HC","ALL"))
CODE
data <- unstack(data, form=X~Y)# easier to read than split
Nmax <- do.call(max, lapply(data,length))
sapply(data, "[", seq(Nmax))# "borrowed" from other answer in SO