我难以取消列出元素。我的变量如下所示:
> file
[[1]]
loc recorded_at fields.b64_value fields.b64_value fields.b64_value
1 9.03913, 45.61335 1.476451e+12 AAAC1g== <NA> <NA>
2 9.03924, 45.61362 1.476451e+12 AAAM+Q== <NA> <NA>
3 9.03995, 45.61365 1.476451e+12 AAAL2A== <NA> <NA>
4 9.04005, 45.61340 1.476451e+12 <NA> <NA> <NA>
5 9.04017, 45.61406 1.476451e+12 AAAUGg== <NA> <NA>
6 9.03949, 45.61419 1.476451e+12 AABLBw== <NA> <NA>
7 9.03496, 45.61319 1.476451e+12 <NA> AAAABA== <NA>
8 9.03440, 45.61295 1.476451e+12 AAArMQ== <NA> <NA>
9 9.03448, 45.61285 1.476451e+12 <NA> <NA> <NA>
10 9.03495, 45.61241 1.476451e+12 AAAAAA== <NA> AA==
[[2]]
loc recorded_at fields.b64_value fields.b64_value fields.b64_value
1 9.03553, 45.61197 1.476451e+12 AABUkQ== AAAAAg== <NA>
2 9.03559, 45.61188 1.476451e+12 <NA> <NA> <NA>
3 9.03606, 45.61129 1.476451e+12 AAAcSQ== <NA> <NA>
4 9.03712, 45.61127 1.476451e+12 <NA> <NA> <NA>
5 9.04059, 45.61095 1.476451e+12 <NA> <NA> <NA>
6 9.04115, 45.61091 1.476451e+12 <NA> <NA> <NA>
7 9.04440, 45.61064 1.476451e+12 <NA> <NA> <NA>
8 9.04444, 45.61067 1.476451e+12 AAAmaQ== <NA> <NA>
9 9.04456, 45.61115 1.476451e+12 AABq3g== <NA> <NA>
10 9.04445, 45.61179 1.476451e+12 AABKuQ== <NA> <NA>
11 9.04303, 45.61281 1.476451e+12 AABY6Q== <NA> <NA>
12 9.04010, 45.61327 1.476451e+12 <NA> <NA> <NA>
13 9.04009, 45.61331 1.476451e+12 AAAmBA== <NA> <NA>
14 9.03989, 45.61365 1.476452e+12 <NA> <NA> AA==
15 9.03989, 45.61365 1.476452e+12 AAAAAA== <NA> <NA>
> typeof(file)
[1] "list"
我想从这个列表中创建一个只包含前三列的数据框,我尝试过使用unlist函数,但我没有成功,还有什么我可以尝试的吗?
答案 0 :(得分:6)
您只能使用base
R:
do.call(rbind,lapply(file,function(x) x[,1:3]))
工作示例:
file <- lapply(1:2,function(x)
data.frame(a=sample(1:100,10),b=sample(1:100,10),c=sample(1:100,10),d=sample(1:100,10)))
> file
[[1]]
a b c d
1 58 80 45 41
2 100 64 25 11
3 3 97 51 18
4 83 45 77 70
5 13 26 56 84
6 93 8 80 5
7 91 86 91 58
8 36 96 90 67
9 27 41 48 68
10 45 75 1 27
[[2]]
a b c d
1 22 37 45 73
2 97 15 93 70
3 54 99 54 46
4 28 75 60 5
5 40 9 95 41
6 58 69 21 36
7 18 40 98 27
8 92 33 29 4
9 76 100 46 32
10 15 47 36 45
> do.call(rbind,lapply(file,function(x) x[,1:3]))
a b c
1 58 80 45
2 100 64 25
3 3 97 51
4 83 45 77
5 13 26 56
6 93 8 80
7 91 86 91
8 36 96 90
9 27 41 48
10 45 75 1
11 22 37 45
12 97 15 93
13 54 99 54
14 28 75 60
15 40 9 95
16 58 69 21
17 18 40 98
18 92 33 29
19 76 100 46
20 15 47 36
答案 1 :(得分:3)
我们可以使用tidyverse
library(tidyverse)
lst %>%
map_df(~.[1:3])
set.seed(24)
lst <- lapply(1:2,function(x) data.frame(a=sample(1:100,10),b=sample(1:100,10),
c=sample(1:100,10),d=sample(1:100,10)))
答案 2 :(得分:1)
> file <- lapply(1:2,function(x)
+ data.frame(a=sample(1:100,10),b=sample(1:100,10),c=sample(1:100,10),d=sample(1:100,10)))
> file
[[1]]
a b c d
1 23 1 23 38
2 2 14 26 11
3 1 61 31 81
4 37 46 64 100
5 65 38 39 19
6 53 13 6 4
7 88 37 74 28
8 73 67 91 70
9 63 91 12 51
10 66 5 57 63
[[2]]
a b c d
1 12 91 14 21
2 20 97 97 57
3 93 95 88 82
4 59 65 2 23
5 55 74 47 87
6 98 48 13 89
7 9 80 33 34
8 45 26 75 54
9 47 55 82 85
10 79 63 28 32
> a=lapply(file,function(x){x[1:3]})
> a
[[1]]
a b c
1 23 1 23
2 2 14 26
3 1 61 31
4 37 46 64
5 65 38 39
6 53 13 6
7 88 37 74
8 73 67 91
9 63 91 12
10 66 5 57
[[2]]
a b c
1 12 91 14
2 20 97 97
3 93 95 88
4 59 65 2
5 55 74 47
6 98 48 13
7 9 80 33
8 45 26 75
9 47 55 82
10 79 63 28
> library(plyr)
> rbind.fill(a)
a b c
1 23 1 23
2 2 14 26
3 1 61 31
4 37 46 64
5 65 38 39
6 53 13 6
7 88 37 74
8 73 67 91
9 63 91 12
10 66 5 57
11 12 91 14
12 20 97 97
13 93 95 88
14 59 65 2
15 55 74 47
16 98 48 13
17 9 80 33
18 45 26 75
19 47 55 82
20 79 63 28
处理数据问题