将包含多个列的列表转换为数据帧

时间:2017-07-11 14:31:59

标签: r list dataframe lapply sapply

我有一个列表,在列表的每个实体中都有多个列。我想提取两个列并将每个实体转换为单行,因为我想用另一个与实体具有相同行数的数据帧来cbind()。

这是我的清单:

[[1]]
                                                 spotify                                                      href                     id
1 https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg 0TnOYISbd1XYRBk9myaseg
2 https://open.spotify.com/artist/2cy1zPcrFcXAJTP0APWewL https://api.spotify.com/v1/artists/2cy1zPcrFcXAJTP0APWewL 2cy1zPcrFcXAJTP0APWewL
           name   type                                   uri
1       Pitbull artist spotify:artist:0TnOYISbd1XYRBk9myaseg
2 Gente De Zona artist spotify:artist:2cy1zPcrFcXAJTP0APWewL


[[2]]
                                                 spotify                                                      href                     id
1 https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg 0TnOYISbd1XYRBk9myaseg
2 https://open.spotify.com/artist/1noWnd8QFQD9VLxWEeo4Zf https://api.spotify.com/v1/artists/1noWnd8QFQD9VLxWEeo4Zf 1noWnd8QFQD9VLxWEeo4Zf
         name   type                                   uri
1     Pitbull artist spotify:artist:0TnOYISbd1XYRBk9myaseg
2 Don Miguelo artist spotify:artist:1noWnd8QFQD9VLxWEeo4Zf

[[3]]
                                                 spotify                                                      href                     id
1 https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg 0TnOYISbd1XYRBk9myaseg
2 https://open.spotify.com/artist/7iJrDbKM5fEkGdm5kpjFzS https://api.spotify.com/v1/artists/7iJrDbKM5fEkGdm5kpjFzS 7iJrDbKM5fEkGdm5kpjFzS
3 https://open.spotify.com/artist/37G8DfNgO4mQ3PKh5droSo https://api.spotify.com/v1/artists/37G8DfNgO4mQ3PKh5droSo 37G8DfNgO4mQ3PKh5droSo
                    name   type                                   uri
1                Pitbull artist spotify:artist:0TnOYISbd1XYRBk9myaseg
2                Sensato artist spotify:artist:7iJrDbKM5fEkGdm5kpjFzS
3 Osmani Garcia "La Voz" artist spotify:artist:37G8DfNgO4mQ3PKh5droSo

[[4]]
                                                 spotify                                                      href                     id
1 https://open.spotify.com/artist/2cy1zPcrFcXAJTP0APWewL https://api.spotify.com/v1/artists/2cy1zPcrFcXAJTP0APWewL 2cy1zPcrFcXAJTP0APWewL
2 https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg 0TnOYISbd1XYRBk9myaseg
           name   type                                   uri
1 Gente De Zona artist spotify:artist:2cy1zPcrFcXAJTP0APWewL
2       Pitbull artist spotify:artist:0TnOYISbd1XYRBk9myaseg

[[5]]
                                                 spotify                                                      href                     id
1 https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg 0TnOYISbd1XYRBk9myaseg
2 https://open.spotify.com/artist/5fjXwEPUkg5ucxmw4TpurV https://api.spotify.com/v1/artists/5fjXwEPUkg5ucxmw4TpurV 5fjXwEPUkg5ucxmw4TpurV
     name   type                                   uri
1 Pitbull artist spotify:artist:0TnOYISbd1XYRBk9myaseg
2   Micha artist spotify:artist:5fjXwEPUkg5ucxmw4TpurV

[[6]]
                                                 spotify                                                      href                     id
1 https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg 0TnOYISbd1XYRBk9myaseg
2 https://open.spotify.com/artist/7wU2WGCJ8HxkekHHE2QLul https://api.spotify.com/v1/artists/7wU2WGCJ8HxkekHHE2QLul 7wU2WGCJ8HxkekHHE2QLul
     name   type                                   uri
1 Pitbull artist spotify:artist:0TnOYISbd1XYRBk9myaseg
2   Fuego artist spotify:artist:7wU2WGCJ8HxkekHHE2QLul

所以我基本上想要在列表中的每个实体中调用第3和第4列,并将输出放在数据帧中。由于列表中每个实体的行数不同,因此我希望将每个实体组合到具有两列的新数据框中的自己的行:idname

目标是让最终输出看起来像这样:

                                           id                           name
0TnOYISbd1XYRBk9myaseg, 2cy1zPcrFcXAJTP0APWewL        Pitbull, Gente De Zona
0TnOYISbd1XYRBk9myaseg, 1noWnd8QFQD9VLxWEeo4Zf          Pitbull, Don Miguelo
0TnOYISbd1XYRBk9myaseg, 7iJrDbKM5fEkGdm5kpjFzS, 37G8DfNgO4mQ3PKh5droSo Pitbull, Sensato, Osmani Garcia "La Voz"
2cy1zPcrFcXAJTP0APWewL, 0TnOYISbd1XYRBk9myaseg        Gente De Zona, Pitbull
0TnOYISbd1XYRBk9myaseg, 5fjXwEPUkg5ucxmw4TpurV                Pitbull, Micha
0TnOYISbd1XYRBk9myaseg, 7wU2WGCJ8HxkekHHE2QLul                Pitbull, Fuego

1 个答案:

答案 0 :(得分:1)

我们可以使用list

遍历lapply
do.call(rbind, lapply(lst, function(x) do.call(cbind, lapply(x[c('id', 'name')], toString))))