用NA替换列表中的所有值(保留标题)

时间:2017-08-17 18:38:26

标签: r list replace na

我有一个看起来有很多部分的列表。我的目标是用NA替换每个$部分中的值。我想保留标题。

列表如下所示:

$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

如何将此列表转换为每行只有NA值?

$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

任何帮助都可以解决这个问题。我知道如何替换数据帧的值而不是列表的值。

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为这样做会: list_2 <- purrr::map(list_1, ~replace(.x, !is.na(.x), NA))