4

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?

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
Marco De Virgilis
  • 982
  • 1
  • 9
  • 29

3 Answers3

7

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
989
  • 12,579
  • 5
  • 31
  • 53
Val
  • 6,585
  • 5
  • 22
  • 52
  • 1
    Could also simplify further with: ``ldply(file, '[', 1:3)`` – Adam Quek Jun 13 '17 at 08:25
  • 1
    you should add this needs the `plyr` package – Val Jun 13 '17 at 08:27
  • Thank you very much for your help, unfortunately, this is the output of both the commands: `> ldply(file, '[', 1:3) Error in allocate_column(df[[var]], nrows, dfs, var) : Data frame column 'fields' not supported by rbind.fill > do.call(rbind,lapply(file,function(x) x[,1:3])) Error in `row.names<-.data.frame`(`*tmp*`, value = value) : duplicate 'row.names' are not allowed In addition: Warning message: non-unique values when setting 'row.names': ‘1’, ‘10’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ ` – Marco De Virgilis Jun 13 '17 at 08:51
  • Seems to be a problem with your data. Try casting it to a `data.frame` with `do.call(rbind,lapply(file,function(x) as.data.frame(x[,1:3])))`. If that doesn't work maybe share the output of `lapply(file,rownames)` – Val Jun 13 '17 at 09:01
  • The solution proposed by Val works fine, there is only one added complication. In this example all the elements in the original list have the same structure, first column,a second column, b etc... This means that if I want to rbind the column a and b with the command `do.call(rbind,lapply(file,function(x) x[,1:2]))` I get the binding of the first two column, in this case a and b, but what if the structure of the element in the list is not fixed, eg abcd, bcda, acdb, and i still want the columns a and b, is there a way to call them by name? – Marco De Virgilis Jun 29 '17 at 07:53
  • Sure, just use a vector of column names: `do.call(rbind,lapply(file,function(x) x[,c('a','b')]))` – Val Jun 29 '17 at 08:09
5

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)))
akrun
  • 874,273
  • 37
  • 540
  • 662
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

This should take care of data issues as per R: rbind multiple data sets

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60