如何在R中重组数据帧

时间:2019-01-30 12:53:58

标签: r

例如,给定以下数据结构

v1 = c("DL1",    "", "DL3", "DL4",   "") 
v2 = c("DL1", "DL2", "DL3",    "","DL5") 
v3 = c("DL1", "DL2", "DL3",    "","DL5") 
v4 = c(   "",    "", "DL3",    "","DL5") 
v5 = c(   "",    "",    "",    "","DL5") 
data1 = rbind(v1,v2,v3,v4,v5)

我要生成

vt1 = c("DL1", "DL3", "DL4",    "","") 
vt2 = c("DL1", "DL2", "DL3", "DL5","") 
vt3 = c("DL1", "DL2", "DL3", "DL5","") 
vt4 = c("DL3",   "" ,    "",    "","") 
vt5 = c("DL5",   "" ,    "",    "","")  
data2 = rbind(vt1,vt2,vt3,vt4,vt5)

1 个答案:

答案 0 :(得分:1)

可能的解决方法是这个。

// Easy to use
context.BulkSaveChanges();

// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);

// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);

// Customize Bulk Operations
context.BulkInsert(customers, options => {
   options => options.IncludeGraph = true;
});
context.BulkMerge(customers, options => {
   options.ColumnPrimaryKeyExpression = 
        customer => customer.Code;
});

要获取行名:

m <- data.frame(t(rbind(v1,v2,v3,v4,v5)), stringsAsFactors=FALSE)
mx <- t(sapply(m, function(x) {
  s <- x[x != ""]
  e <- 5 - length(s)
  return(c(s, rep("", e)))
}))

生产

rownames(mx) <- gsub("v", "vt", rownames(mx))