这有效,但它太长了。我很确定有更简单的方法。
dat <- read.table(text='Col1 Col2
Name1 Attr1=10;Attr2=24; Attr3=5;Attr4=9;
Name2 Attr1=1;Attr3=2.4; Attr4=16;Attr5=90;
Name3 Attr1=2;Attr2=45; Attr4=122;Attr6=120;',
header=TRUE,stringsAsFactors=FALSE)
res <- do.call(cbind,lapply(dat,function(x)
do.call(rbind,strsplit(x,split=";"))))
indx <- gsub('Attr([0-9]+).*','\\1',res)
vals <- gsub('.*=([0-9]+)','\\1',res)
mm <- max(as.integer(indx))
M <- matrix(NA_real_,nrow=nrow(res),ncol=mm)
for(i in seq_len(nrow(M)))
M[i,as.integer(indx[i,])] <- as.numeric(vals[i,])
rownames(M) <- rownames(dat)
colnames(M) <- paste0('Attr',seq_len(mm))
Attr1 Attr2 Attr3 Attr4 Attr5 Attr6
Name1 10 24 5.0 9 NA NA
Name2 1 NA 2.4 16 90 NA
Name3 2 45 NA 122 NA 120