1
votes

I have a list that has multiple columns within each entity of the list. I want to extract two of the columns and convert each entity into a single row because I want to cbind() this with another dataframe that has the same number of rows as entities.

Here is my list:

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

So I basically want to call the 3rd and 4th column in each entity within the list and have the output in a dataframe. Since the number of rows are different for each entity of the list, I want to combine each entity into it's own row in a new dataframe with two columns: the id and the name.

The goal is to have the final output look like this:

                                           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
try do.call(rbind,lapply(lst,function(x) x[,2:3])) with lst being your listVal

1 Answers

1
votes

We can loop through the list with lapply

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