如何通过R中的名称将矢量转换为矩阵?

时间:2015-06-06 12:39:46

标签: r matrix

我有这个载体

   ID   var2     ID   var2   var1     ID   var2     ID   var3 
"1000"    "1" "1001"    "1"    "1" "1002"    "7" "1003"    "3" 

可以从

获得
x=c("1000","1","1001","1","1","1002","7","1003","3")
names(x)=c("ID","var2","ID","var2","var1","ID","var2","ID","var3")

我将它转换为维度4x4的矩阵:

ID        var1     var2    var3
"1000"     NA       "1"     NA
"1001"     "1"      "1"     NA
"1002"     NA       "7"     NA
"1003"     NA       NA     "3"  

你能帮我吗?

2 个答案:

答案 0 :(得分:3)

我们可以split向量' x'通过基于“ID'”match出现的分组向量,将唯一名称(' nm1')与order列表元素的名称和然后rbind获得预期的输出

 nm1 <- sort(unique(names(x)))
 do.call(rbind,lapply(split(x, cumsum(grepl('ID', names(x)))), function(y)
        setNames(y[match(nm1, names(y))], nm1)
        ))
 # ID     var1 var2 var3
 #1 "1000" NA   "1"  NA  
 #2 "1001" "1"  "1"  NA  
 #3 "1002" NA   "7"  NA  
 #4 "1003" NA   NA   "3" 

创建空矩阵后使用row/col索引。

indx <- match(names(x), nm1)
m1 <- matrix(, nrow= max(tabulate(indx)), ncol=length(nm1), 
             dimnames=list(NULL, nm1))
m1[cbind(cumsum(indx==1), indx)] <- x

答案 1 :(得分:2)

既不漂亮也不快,但您可以使用data.table::rbindlist

执行此类操作
library(data.table)
# Create list of lists splitting data by ID and convert to data.table
dt <- rbindlist(tapply(x, cumsum(names(x) == "ID"), as.list), fill=TRUE)
# Ensure column order 
setcolorder(dt, c('ID', 'var1', 'var2', 'var3'))
# Convert to matrix
as.matrix(dt)