在R

时间:2018-04-18 21:55:02

标签: r cbind

这是我的代码

library(readxl)
library(dplyr)
library(rowr)

filenames = list.files(pattern="*.xlsx")

readdata <- function(filename) {
  df <- read_excel(filename)
  vec <- select(df, ends_with("Course total (Letter)"))
  return(vec)
}



result <-lapply(filenames, readdata)

,结果是一个包含不同行数的3的列表,如下所示

> > result [[1]]
> # A tibble: 24 x 1    `Course total (Letter)`    <chr>                    1 PASS                     2 FAIL                     3 PASS          
> 4 FAIL                     5 FAIL                     6 FAIL          
> 7 FAIL                     8 FAIL                     9 FAIL          
> 10 FAIL                   
> # ... with 14 more rows
> 
> [[2]]
> # A tibble: 87 x 1    `Course total (Letter)`    <chr>                    1 Pass                     2 Pass                     3 Pass          
> 4 Fail                     5 Fail                     6 Pass          
> 7 Fail                     8 Fail                     9 Fail          
> 10 Fail                   
> # ... with 77 more rows
> 
> [[3]]
> # A tibble: 23 x 1    `Course total (Letter)`    <chr>                    1 Fail                     2 Fail                     3 Fail          
> 4 Fail                     5 Pass                     6 Fail          
> 7 Pass                     8 Pass                     9 Fail          
> 10 Fail                   
> # ... with 13 more rows

我想将所有3个元素合并到一个数据框中,所以我尝试了这个

result <- do.call("cbind.fill", result)

现在,结果变为

 result
   Course.total..Letter. Course.total..Letter. Course.total..Letter.
1                   PASS                  Pass                  Fail
2                   FAIL                  Pass                  Fail
3                   PASS                  Pass                  Fail
4                   FAIL                  Fail                  Fail
5                   FAIL                  Fail                  Pass
6                   FAIL                  Pass                  Fail
7                   FAIL                  Fail                  Pass
8                   FAIL                  Fail                  Pass
9                   FAIL                  Fail                  Fail
10                  FAIL                  Fail                  Fail
11                  FAIL                  Fail                  Fail
12                  FAIL                  Fail                  Fail
13                  FAIL                  Fail                  Fail
14                  PASS                  Fail                  Pass
15                  FAIL                  Fail                  Fail
16                  PASS                  Fail                  Fail
17                  FAIL                  Fail                  Fail
18                  FAIL                  Pass                  Fail
19                  FAIL                  Pass                  Fail
20                  PASS                  Fail                  Fail
21                  FAIL                  Fail                  Fail
22                  FAIL                  Fail                  Pass
23                  FAIL                  Fail                  Fail
24                  FAIL                  Fail                  Fail
25                  PASS                  Fail                  Fail
26                  FAIL                  Fail                  Fail
27                  PASS                  Fail                  Fail
28                  FAIL                  Fail                  Pass
29                  FAIL                  Fail                  Fail
30                  FAIL                  Fail                  Pass
31                  FAIL                  Fail                  Pass
32                  FAIL                  Pass                  Fail
33                  FAIL                  Pass                  Fail
34                  FAIL                  Fail                  Fail
35                  FAIL                  Fail                  Fail
36                  FAIL                  Fail                  Fail
37                  FAIL                  Pass                  Pass
38                  PASS                  Fail                  Fail
39                  FAIL                  Fail                  Fail
40                  PASS                  Pass                  Fail
41                  FAIL                  Fail                  Fail
42                  FAIL                  Fail                  Fail
43                  FAIL                  Fail                  Fail
44                  PASS                  Fail                  Fail
45                  FAIL                  Fail                  Pass
46                  FAIL                  Pass                  Fail
47                  FAIL                  Pass                  Fail
48                  FAIL                  Pass                  Fail
49                  PASS                  Fail                  Fail
50                  FAIL                  Fail                  Fail
51                  PASS                  Fail                  Pass
52                  FAIL                  Fail                  Fail
53                  FAIL                  Pass                  Pass
54                  FAIL                  Fail                  Pass
55                  FAIL                  Pass                  Fail
56                  FAIL                  Fail                  Fail
57                  FAIL                  Pass                  Fail
58                  FAIL                  Fail                  Fail
59                  FAIL                  Fail                  Fail
60                  FAIL                  Pass                  Pass
61                  FAIL                  Fail                  Fail
62                  PASS                  Fail                  Fail
63                  FAIL                  Fail                  Fail
64                  PASS                  Fail                  Fail
65                  FAIL                  Pass                  Fail
66                  FAIL                  Fail                  Fail
67                  FAIL                  Fail                  Fail
68                  PASS                  Fail                  Pass
69                  FAIL                  Fail                  Fail
70                  FAIL                  Fail                  Fail
71                  FAIL                  Fail                  Fail
72                  FAIL                  Fail                  Fail
73                  PASS                  Fail                  Fail
74                  FAIL                  Fail                  Pass
75                  PASS                  Pass                  Fail
76                  FAIL                  Fail                  Pass
77                  FAIL                  Fail                  Pass
78                  FAIL                  Fail                  Fail
79                  FAIL                  Fail                  Fail
80                  FAIL                  Pass                  Fail
81                  FAIL                  Pass                  Fail
82                  FAIL                  Fail                  Fail
83                  FAIL                  Pass                  Pass
84                  FAIL                  Fail                  Fail
85                  FAIL                  Pass                  Fail
86                  PASS                  Fail                  Fail
87                  FAIL                  Fail                  Fail

我希望将空行填充为NA。在这种情况下,每列重复一次以匹配87行。怎么做?

1 个答案:

答案 0 :(得分:1)

您需要为fill设置cbind.fill参数。您可以通过在列表中添加名为fill来完成此操作。

result[["fill"]] <- NA
result <- do.call("cbind.fill",result)

示例:

k <- list(c(1:23),c(1:87),c(1:24))
do.call("cbind.fill",k)
#behaves as the code in the question does

k <- list(c(1:23),c(1:87),c(1:24))
k[["fill"]] <-NA
do.call("cbind.fill",k)
#behaves as desired