r语言 - 将列中的值替换为 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]

如果没有数据来测试这一点,我会尝试:

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

如此处所述,NA 的功能更像您似乎正在尝试执行的操作,而 NULL 则提供不同的功能。如果只想让值只说"NULL",而不是像 NULL 那样函数,请将其视为字符值。

相关内容

  • 没有找到相关文章

最新更新