如何组合所有列表并创建唯一名称

时间:2018-03-24 16:06:12

标签: r

我有这样的数据

 ldf <- list(structure(list(Abund = c("BROS", "KIS", "TTHS", 
"MKS"), `Value: F111: cold, Sample1` = c("1.274e7", "", 
"", "2.301e7"), `Value: F111: warm, Sample1` = c("", "", 
"", "")), .Names = c("Abund", "Value: F111: cold, Sample1", 
"Value: F111: warm, Sample1"), row.names = c(NA, 4L), class = "data.frame"), 
    structure(list(Abund = c("BROS", "TMS", "KIS", 
    "HERS"), `Value: F216: cold, Sample2` = c("1.670e6", 
    "4.115e7", "", "1.302e7"), `Value: F216: warm, Sample2` = c("", 
    "2.766e7", "", "1.396e7")), .Names = c("Abund", "Value: F216: cold, Sample2", 
    "Value: F216: warm, Sample2"), row.names = c(NA, 4L), class = "data.frame"), 
    structure(list(Abund = c("BROS", "TMS", "KIS", 
    "HERS"), `Value: F655: cold, Sample3` = c("7.074e4", 
    "1.038e7", "", "7.380e5"), `Value: F655: warm, Sample3` = c("", 
    "6.874e6", "", "7.029e5")), .Names = c("Abund", "Value: F655: cold, Sample3", 
    "Value: F655: warm, Sample3"), row.names = c(NA, 4L), class = "data.frame")) 

我想在这个案例中使用一个独特的名字Abund 然后我尝试将数据接近它,如下所示 所以欲望输出就像这样

Abund   coldsample1 Sample1 coldSample2 warmSample2 coldSample3 warmSample3
BROS    1.27E+07        1.67E+06        7.07E+04    
TMS                     4.12E+07        2.77E+07    1.04E+07    6.87E+06
HERS                    1.30E+07        1.40E+07    7.38E+05    7.03E+05
MKS     2.30E+07                    
KIS                     
TTHS

2 个答案:

答案 0 :(得分:1)

在Base R中,你可以做这样的事情......

var object = {};
Object.defineProperty(object, 'first', {
     value: 37,
     writable: false,
     enumerable: true,
     configurable: true
});
console.log('first property: ' + object.first);

答案 1 :(得分:0)

data.table的帮助下你可以做一些帮助(编辑后根据不同长度的数据帧列表工作)

x <- rbindlist(lapply(ldf, function(i) cbind(i["Abund"], stack(i[2:3]), row.names = NULL))) #create a data table with all the unique values of the Abund accrosss the 3 different list of data.frames you provided
y <- dcast(x, Abund~ind, value.var="values") #cast the long format data into a usasble form
names(y) <- gsub(".*:", "", names(y)); names(y) <- gsub(", ", "", names(y)) #get nicer variable names
(y <- y[,lapply(.SD,function(j){ifelse(j=="", NA, j)})]) #prints the end table with a correct and complete list of NAs
#   Abund  coldSample1  warmSample1  coldSample2  warmSample2  coldSample3  warmSample3
#1:  BROS      1.274e7           NA      1.670e6           NA      7.074e4           NA
#2:  HERS           NA           NA      1.302e7      1.396e7      7.380e5      7.029e5
#3:   KIS           NA           NA           NA           NA           NA           NA
#4:   MKS      2.301e7           NA           NA           NA           NA           NA
#5:   TMS           NA           NA      4.115e7      2.766e7      1.038e7      6.874e6
#6:  TTHS           NA           NA           NA           NA           NA           NA