0

我有一个数据框,我想用 NULL 替换包含值“2018”的列中的所有值。

我有一个数据集,其中列中的每个值都是一个列表。也包括 NULL。其中一个值不是列表,我想用 NULL 替换它。如果我用 NA 替换它,那么该列中的数据类型是混合的。

如果我有如下所示的列,如何将包含 2018 的值替换为 NULL 而不是 NA?

 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

0

没有数据来测试这个,我会尝试:

df$COL1<-ifelse(grepl("2018", df$COL1),"NULL",df$COL1)

如此处所述NA 功能更像您似乎正在尝试做的事情,而 NULL 提供不同的功能。如果您只是希望该值只是说“NULL”而不是像 NULL 那样起作用,请将其视为字符值。

于 2018-03-13T22:46:42.030 回答