0

I have a list that looks that has many parts to it. My goal is to replace the values within each $section with NA. I want to keep the headers though.

The list looks like this:

$href
[1] "https://api.spotify.com/v1/albums/4vucVTj5GjNV0MllVjsxo5/tracks?offset=0&limit=50"

$items

artists
1 https://open.spotify.com/artist/2ueoLVCXQ948OfhVvAy3Nn, https://api.spotify.com/v1/artists/2ueoLVCXQ948OfhVvAy3Nn, 2ueoLVCXQ948OfhVvAy3Nn, Perfume Genius, artist, spotify:artist:2ueoLVCXQ948OfhVvAy3Nn
2 https://open.spotify.com/artist/2ueoLVCXQ948OfhVvAy3Nn, https://api.spotify.com/v1/artists/2ueoLVCXQ948OfhVvAy3Nn, 2ueoLVCXQ948OfhVvAy3Nn, Perfume Genius, artist, spotify:artist:2ueoLVCXQ948OfhVvAy3Nn

available_markets
1 AD, AR, AT, AU, BE, BG, BO, BR, CA, CH, CL, CO, CR, CY, CZ, DE, DK, DO, EC, EE, ES, FI, FR, GB, GR, GT, HK, HN, HU, ID, IE, IS, IT, JP, LI, LT, LU, LV, MC, MT, MX, MY, NI, NL, NO, NZ, PA, PE, PH, PL, PT, PY, SE, SG, SK, SV, TR, TW, US, UY
2 AD, AR, AT, AU, BE, BG, BO, BR, CA, CH, CL, CO, CR, CY, CZ, DE, DK, DO, EC, EE, ES, FI, FR, GB, GR, GT, HK, HN, HU, ID, IE, IS, IT, JP, LI, LT, LU, LV, MC, MT, MX, MY, NI, NL, NO, NZ, PA, PE, PH, PL, PT, PY, SE, SG, SK, SV, TR, TW, US, UY
  disc_number duration_ms explicit                                               
spotify
1           1      166946    FALSE https://open.spotify.com/track/2QX2OeVkckAavMTJtbV7GX
2           1      224620    FALSE https://open.spotify.com/track/15yWH0t80xio9hCjOZpalj
                                                  href                     
id
1 https://api.spotify.com/v1/tracks/2QX2OeVkckAavMTJtbV7GX 2QX2OeVkckAavMTJtbV7GX
2 https://api.spotify.com/v1/tracks/15yWH0t80xio9hCjOZpalj 15yWH0t80xio9hCjOZpalj
                                                 name
1         Slip Away - Recorded at Spotify Studios NYC
2 Body's In Trouble - Recorded at Spotify Studios NYC

preview_url track_number  type
1 https://p.scdn.co/mp3-preview/fc1e8a296749237d7558ecc09bc5244251d033de?cid=c432b36c21724d2989baf7d4d8a6bfd3            1 track
2 https://p.scdn.co/mp3-preview/ac154324daedaff11d9806367bc980293dfb491b?cid=c432b36c21724d2989baf7d4d8a6bfd3            2 track
                                   uri
1 spotify:track:2QX2OeVkckAavMTJtbV7GX
2 spotify:track:15yWH0t80xio9hCjOZpalj

$limit
[1] 50

$`next`
named list()

$offset
[1] 0

$previous
named list()

$total
[1] 2

How do I convert this list to have only NA values for each row like this?

$href
[1] NA

$items

artists
1 NA
2 NA

available_markets
1 NA
2 NA
  disc_number duration_ms explicit                                               
spotify
1 NA
2 NA
                                                  href                     
id
1 NA
2 NA
                                                 name
1                                                  NA
2                                                  NA

preview_url track_number  type
1 NA
2 NA
                                   uri
1                                   NA
2                                   NA

$limit
[1] NA

$`next`
NA

$offset
[1] NA

$previous
NA

$total
[1] NA

Any help would be great to solve this issue. I know how to replace values for a dataframe but not for a list.

Thanks!

nak5120
  • 4,089
  • 4
  • 35
  • 94
  • It would be easier to help if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Share a `dput()` rather than just the printed object. And is this really even needed? What exactly are you trying to do? – MrFlick Aug 17 '17 at 18:45
  • I suppose every element in the list is a data frame? If so, maybe you can use `lapply(you_list, fun)`, where `fun` is a function how you replace values for a dataframe? – Sean Lin Aug 17 '17 at 18:47
  • 1
    I think this will do it `list_2 <- purrr::map(list_1, ~replace(.x, !is.na(.x), NA))` – roarkz Aug 17 '17 at 18:48
  • Thanks @roarkz that did it! – nak5120 Aug 17 '17 at 18:51
  • Can you put that down as an answer so I can mark it as an answer? – nak5120 Aug 17 '17 at 18:51
  • 1
    My `relist` answer didn't recreate the `data.frame` accurately. If you're looking for a base R approach, you could alternatively use `lapply(your_list, function(y) {y[] <- NA; y})` – talat Aug 17 '17 at 19:36

1 Answers1

2

I think this will do it: list_2 <- purrr::map(list_1, ~replace(.x, !is.na(.x), NA))

roarkz
  • 811
  • 10
  • 22