0

値「2018」を含む列のすべての値を NULL に置き換えたいデータフレームがあります。

列のすべての値がリストであるデータセットがあります。NULL も含まれています。値の 1 つがリストではないため、NULL に置き換えたいと考えています。NA に置き換えると、その列のデータ型が混在します。

以下のような列がある場合、2018 を含む値を NA ではなく NULL に置き換えるにはどうすればよいですか?

 spend         actions 
 176.2         2018-02-24
166.66         list(action_type = c("landing_page_view", "link_click", "offsit...         
153.89         list(action_type = c("landing_page_view", "like", "link_click",...
156.54         list(action_type = c("landing_page_view", "like", "link_click",...
254.95         list(action_type = c("landing_page_view", "like", "link_click",...
   374         list(action_type = c("landing_page_view", "like", "link_click",...
353.29         list(action_type = c("landing_page_view", "like", "link_click",...
  0.41         NULL

再現可能な例:

structure(list(spend = c("176.2", "166.66", "153.89", "156.54", 
"254.95", "374", "353.29", "0.41"), actions = list("2018-02-24", 
    structure(list(action_type = c("landing_page_view", "link_click", 
    "offsite_conversion.fb_pixel_add_to_cart", 
"offsite_conversion.fb_pixel_purchase", 
    "offsite_conversion.fb_pixel_search", 
"offsite_conversion.fb_pixel_view_content", 
    "post", "post_reaction", "page_engagement", "post_engagement", 
    "offsite_conversion"), value = c("179", "275", "212", "18", 
    "269", "1434", "1", "17", "293", "293", "1933")), .Names = c("action_type", 
    "value"), class = "data.frame", row.names = c(NA, 11L)), 
    structure(list(action_type = c("landing_page_view", "like", 
    "link_click", "offsite_conversion.fb_pixel_add_to_cart", 
    "offsite_conversion.fb_pixel_purchase", 
"offsite_conversion.fb_pixel_search", 
    "offsite_conversion.fb_pixel_view_content", "post_reaction", 
    "page_engagement", "post_engagement", "offsite_conversion"
    ), value = c("136", "3", "248", "101", "6", "237", "730", 
    "11", "262", "259", "1074")), .Names = c("action_type", "value"
    ), class = "data.frame", row.names = c(NA, 11L)), structure(list(
        action_type = c("landing_page_view", "like", "link_click", 
        "offsite_conversion.fb_pixel_add_to_cart", 
"offsite_conversion.fb_pixel_purchase", 
        "offsite_conversion.fb_pixel_search", 
"offsite_conversion.fb_pixel_view_content", 
        "post", "post_reaction", "page_engagement", "post_engagement", 
        "offsite_conversion"), value = c("95", "1", "156", "91", 
        "5", "83", "532", "1", "13", "171", "170", "711")), .Names = 
c("action_type", 
    "value"), class = "data.frame", row.names = c(NA, 12L)), 
    structure(list(action_type = c("landing_page_view", "like", 
    "link_click", "offsite_conversion.fb_pixel_add_to_cart", 
    "offsite_conversion.fb_pixel_purchase", 
"offsite_conversion.fb_pixel_search", 
    "offsite_conversion.fb_pixel_view_content", "post_reaction", 
    "page_engagement", "post_engagement", "offsite_conversion"
    ), value = c("178", "4", "243", "56", "4", "138", "437", 
    "19", "266", "262", "635")), .Names = c("action_type", "value"
    ), class = "data.frame", row.names = c(NA, 11L)), structure(list(
        action_type = c("landing_page_view", "like", "link_click", 
        "offsite_conversion.fb_pixel_add_to_cart", 
"offsite_conversion.fb_pixel_purchase", 
        "offsite_conversion.fb_pixel_search", 
"offsite_conversion.fb_pixel_view_content", 
        "post_reaction", "page_engagement", "post_engagement", 
        "offsite_conversion"), value = c("203", "2", "306", "105", 
        "7", "186", "954", "23", "331", "329", "1252")), .Names = 
c("action_type", 
    "value"), class = "data.frame", row.names = c(NA, 11L)), 
    structure(list(action_type = c("landing_page_view", "like", 
     "link_click", "offsite_conversion.fb_pixel_add_to_cart", 
    "offsite_conversion.fb_pixel_purchase", 
"offsite_conversion.fb_pixel_search", 
"offsite_conversion.fb_pixel_view_content", "post", "post_reaction", 
"page_engagement", "post_engagement", "offsite_conversion"
), value = c("241", "4", "320", "106", "3", "240", "789", 
"1", "17", "342", "338", "1138")), .Names = c("action_type", 
"value"), class = "data.frame", row.names = c(NA, 12L)), 
NULL)), .Names = c("spend", "actions"), row.names = c(NA, 
-8L), class = "data.frame")

私の最終的な目標は、この関数をこのデータセットで使用して、action_types を独自の列にすることです。この関数は、リストまたは NULL がアクション列にある場合に機能します。

fb_insights_all<-df %>%
  as.tibble() %>%
  filter(!map_lgl(actions, is.null)) %>%
  unnest() %>%
  right_join(select(df, -actions)) %>%
  spread(action_type, value)

Error: Each column must either be a list of vectors or a list of data frames [actions]
4

1 に答える 1