Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unlist a list R

I am having difficulties unlisting an element. My variable looks like the following:

> 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"

I would like to create a dataframe from this list with only the first three columns, I have tried with the unlist function but I am not being successful, is there anything else I can try?

like image 974
Marco De Virgilis Avatar asked Dec 11 '22 11:12

Marco De Virgilis


2 Answers

You can try this using only base R:

do.call(rbind,lapply(file,function(x) x[,1:3]))

Working example:

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
like image 131
Val Avatar answered Dec 12 '22 23:12

Val


We can do this with tidyverse

library(tidyverse)
lst %>%
     map_df(~.[1:3]) 

data

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)))
like image 35
akrun Avatar answered Dec 12 '22 23:12

akrun