在R中将不同长度的行合并为一

时间:2018-07-04 15:20:15

标签: r dataframe bind

想象一下,我得到了以下结果:

>coef(model1)
                   Estimate  Std. Error    t-value     Pr(>|t|)
    X1           -1.3006813 1.962377480 -0.6628089 5.079128e-01
    X2          -12.1455641 5.838937045 -2.0800985 3.828320e-02
    X3           11.0701123 9.367725395  1.1817290 2.381590e-01
    X4            3.2903167 0.690324322  4.7663346 2.810855e-06
    X5            0.0240804 0.005345124  4.5051151 9.201743e-06
    X6            0.1834814 0.047485944  3.8639097 1.341940e-04

>coef(model2)
                       Estimate  Std. Error    t-value     Pr(>|t|)
        X1           -1.3180103 1.962377480 -0.6628089 5.079128e-01
        X2          -10.5006410 5.838937045 -2.0800985 3.828320e-02

>coef(model6)
                           Estimate  Std. Error    t-value     Pr(>|t|)
            X2           -1.3256782 1.962377480 -0.6628089 5.079128e-01
            X6          -1.56931410 5.838937045 -2.0800985 3.828320e-02    

我要做的是创建此表,其中每一行是每个模型的系数:

 X1      X2    X3      X4     X5     X6
-1.300 -12.145 11.070 3.290  0.0240 0.183
-1.318 -10.500
       -1.325                      -1.569

有什么建议吗? *理想情况下,我想添加一个* if p-value>0.05, ** if p-value>0.01 and *** if <0.01,以便立即将其与library(xtable)添加到乳胶中。

2 个答案:

答案 0 :(得分:1)

我认为这可能会更加简洁,但是一开始您可以使用它。另外,我使用了舍入功能,因此-1.3006813在我的代码中变成了-1.301,而不是1.300。另外,请注意Pr...t..只是列名Pr(>|t|)的读入错误。

library(plyr)
library(dplyr)
library(tidyr)

myfunction <- function(x){
  x %>% 
mutate(Std. = round(Std., 3),
       Std.Adjusted = ifelse(Pr...t.. > 0.05, paste0(Std., "*"), ifelse(Pr...t.. < 0.05 & Pr...t.. > 0.01, paste0(Std., "**"), paste0(Std., "***")))) %>% 
  select(Estimate, Std.Adjusted) %>% 
  spread(Estimate, Std.Adjusted)
}

rbind.fill(lapply(list(mod1,mod2,mod6), myfunction))

       X1        X2     X3      X4       X5       X6
1 -1.301* -12.146** 11.07* 3.29*** 0.024*** 0.183***
2 -1.318* -10.501**   <NA>    <NA>     <NA>     <NA>
3    <NA>   -1.326*   <NA>    <NA>     <NA> -1.569**

数据:

mod1 <- read.table(text = "
Estimate  Std. Error    t-value     Pr(>|t|)
X1           -1.3006813 1.962377480 -0.6628089 5.079128e-01
X2          -12.1455641 5.838937045 -2.0800985 3.828320e-02
X3           11.0701123 9.367725395  1.1817290 2.381590e-01
X4            3.2903167 0.690324322  4.7663346 2.810855e-06
X5            0.0240804 0.005345124  4.5051151 9.201743e-06
X6            0.1834814 0.047485944  3.8639097 1.341940e-04
", h = T)


mod2 <- read.table(text = "
Estimate  Std. Error    t-value     Pr(>|t|)
X1           -1.3180103 1.962377480 -0.6628089 5.079128e-01
X2          -10.5006410 5.838937045 -2.0800985 3.828320e-02
", h = T)

mod6 <- read.table(text = "
Estimate  Std. Error    t-value     Pr(>|t|)
X2           -1.3256782 1.962377480 -0.6628089 5.079128e-01
X6          -1.56931410 5.838937045 -2.0800985 3.828320e-02  
", h = T)

答案 1 :(得分:1)

这有效:

library(tidyverse)

data <- list(model1, model2, model6)

data %>% 
  map_df(rownames_to_column, "Var", .id = 'Model') %>%
  select(Model, Var, Estimate) %>%
  spread(Var, Estimate)

|Model |        X1|         X2|       X3|       X4|        X5|         X6|
|:-----|---------:|----------:|--------:|--------:|---------:|----------:|
|1     | -1.300681| -12.145564| 11.07011| 3.290317| 0.0240804|  0.1834814|
|2     | -1.318010| -10.500641|       NA|       NA|        NA|         NA|
|3     |        NA|  -1.325678|       NA|       NA|        NA| -1.5693141|

数据:

model1 <- read.table(text = "
Estimate  Std.Error    t-value     Pr(>|t|)
X1           -1.3006813 1.962377480 -0.6628089 5.079128e-01
X2          -12.1455641 5.838937045 -2.0800985 3.828320e-02
X3           11.0701123 9.367725395  1.1817290 2.381590e-01
X4            3.2903167 0.690324322  4.7663346 2.810855e-06
X5            0.0240804 0.005345124  4.5051151 9.201743e-06
X6            0.1834814 0.047485944  3.8639097 1.341940e-04
", h = T)


model2 <- read.table(text = "
Estimate  Std.Error    t-value     Pr(>|t|)
X1           -1.3180103 1.962377480 -0.6628089 5.079128e-01
X2          -10.5006410 5.838937045 -2.0800985 3.828320e-02
", h = T)

model6 <- read.table(text = "
Estimate  Std.Error    t-value     Pr(>|t|)
X2           -1.3256782 1.962377480 -0.6628089 5.079128e-01
X6          -1.56931410 5.838937045 -2.0800985 3.828320e-02  
", h = T)

不太清楚星星的意思,但是可以在管道中添加case_when语句来添加星星