others - r - bind_cols放入数据帧,并保留数字名称

我有4个非常短的列表,我想将它们绑定到一个数据帧,我尝试过bind_cols(x),其中x是列表,我也尝试过lapply(x, data.frame) %>% bind_cols()

所需输出:


Var Name ....1 ....2 ....3 ....4


Accuracy 0.8690265 0.8103582 0.7820126 0.7598878


Sensitivity 0.7336562 0.7589099 0.7877551 0.7327766


Specificity 0.8848108 0.8175676 0.7811213 0.7640959


Precision 0.4261603 0.3682604 0.3584030 0.3253012


F1 0.5391459 0.4958904 0.4926611 0.4505777


MCC 0.4925390 0.4352215 0.4252838 0.3688497


AUC 0.8092335 0.7882387 0.7844382 0.7484363



数据:


list(structure(c(0.869026548672566, 0.73365617433414, 0.88481084133258, 


0.426160337552743, 0.53914590747331, 0.492538999353894, 0.80923350783336


), .Dim = c(7L, 1L), .Dimnames = list(c("Accuracy","Sensitivity", 


"Specificity","Precision","F1","MCC","AUC"), NULL)), structure(c(0.810358155114661, 


0.758909853249476, 0.817567567567568, 0.368260427263479, 0.495890410958904, 


0.435221475998339, 0.788238710408522), .Dim = c(7L, 1L), .Dimnames = list(


 c("Accuracy","Sensitivity","Specificity","Precision", 


"F1","MCC","AUC"), NULL)), structure(c(0.782012613106663, 


0.787755102040816, 0.781121317706684, 0.358402971216342, 0.492661135928526, 


0.425283810027502, 0.78443820987375), .Dim = c(7L, 1L), .Dimnames = list(


 c("Accuracy","Sensitivity","Specificity","Precision", 


"F1","MCC","AUC"), NULL)), structure(c(0.759887798036466, 


0.732776617954071, 0.764095917044718, 0.325301204819277, 0.450577663671374, 


0.368849691479186, 0.748436267499395), .Dim = c(7L, 1L), .Dimnames = list(


 c("Accuracy","Sensitivity","Specificity","Precision", 


"F1","MCC","AUC"), NULL)))



编辑:


> bind_cols(x) %>%


+ as.data.frame %>%


+ `row.names<-`(., row.names(x[[1]]))


 X..i.....1 X..i.....2 X..i.....3 X..i.....4


1 0.8690265 0.8103582 0.7820126 0.7598878


2 0.7336562 0.7589099 0.7877551 0.7327766


3 0.8848108 0.8175676 0.7811213 0.7640959


4 0.4261603 0.3682604 0.3584030 0.3253012


5 0.5391459 0.4958904 0.4926611 0.4505777


6 0.4925390 0.4352215 0.4252838 0.3688497


7 0.8092335 0.7882387 0.7844382 0.7484363


> 


> do.call(cbind, x)


 X..i.....1 X..i.....2 X..i.....3 X..i.....4


[1,] 0.8690265 0.8103582 0.7820126 0.7598878


[2,] 0.7336562 0.7589099 0.7877551 0.7327766


[3,] 0.8848108 0.8175676 0.7811213 0.7640959


[4,] 0.4261603 0.3682604 0.3584030 0.3253012


[5,] 0.5391459 0.4958904 0.4926611 0.4505777


[6,] 0.4925390 0.4352215 0.4252838 0.3688497


[7,] 0.8092335 0.7882387 0.7844382 0.7484363



编辑2:

删除行column_to_rownames('rn')和:


map(x, ~ .x %>%


 as.data.frame %>%


 rownames_to_column('rn')) %>% 


 reduce(inner_join, by = 'rn')



给我:


 rn X..i...x X..i...y X..i...x.x X..i...y.y


1 1 0.8690265 0.8103582 0.7820126 0.7598878


2 2 0.7336562 0.7589099 0.7877551 0.7327766


3 3 0.8848108 0.8175676 0.7811213 0.7640959


4 4 0.4261603 0.3682604 0.3584030 0.3253012


5 5 0.5391459 0.4958904 0.4926611 0.4505777


6 6 0.4925390 0.4352215 0.4252838 0.3688497


7 7 0.8092335 0.7882387 0.7844382 0.7484363



时间:

tidyverse函数删除行名。需要分别对行名进行预处理


library(dplyr)


library(purrr)


library(tibble)


map(x, ~ .x %>%


 as.data.frame %>%


 rownames_to_column('rn')) %>% 


 reduce(inner_join, by = 'rn') %>%


 column_to_rownames('rn')


# V1.x V1.y V1.x.x V1.y.y


#Accuracy 0.8690265 0.8103582 0.7820126 0.7598878


#Sensitivity 0.7336562 0.7589099 0.7877551 0.7327766


#Specificity 0.8848108 0.8175676 0.7811213 0.7640959


#Precision 0.4261603 0.3682604 0.3584030 0.3253012


#F1 0.5391459 0.4958904 0.4926611 0.4505777


#MCC 0.4925390 0.4352215 0.4252838 0.3688497


#AUC 0.8092335 0.7882387 0.7844382 0.7484363



或者另一个选择是


bind_cols(x) %>%


 as.data.frame %>%


 `row.names<-`(., row.names(x[[1]]))


# ...1 ...2 ...3 ...4


#Accuracy 0.8690265 0.8103582 0.7820126 0.7598878


#Sensitivity 0.7336562 0.7589099 0.7877551 0.7327766


#Specificity 0.8848108 0.8175676 0.7811213 0.7640959


#Precision 0.4261603 0.3682604 0.3584030 0.3253012


#F1 0.5391459 0.4958904 0.4926611 0.4505777


#MCC 0.4925390 0.4352215 0.4252838 0.3688497


#AUC 0.8092335 0.7882387 0.7844382 0.7484363



base R中它更紧凑


do.call(cbind, x)


# [,1] [,2] [,3] [,4]


#Accuracy 0.8690265 0.8103582 0.7820126 0.7598878


#Sensitivity 0.7336562 0.7589099 0.7877551 0.7327766


#Specificity 0.8848108 0.8175676 0.7811213 0.7640959


#Precision 0.4261603 0.3682604 0.3584030 0.3253012


#F1 0.5391459 0.4958904 0.4926611 0.4505777


#MCC 0.4925390 0.4352215 0.4252838 0.3688497


#AUC 0.8092335 0.7882387 0.7844382 0.7484363



...