读取列表,如数据帧单元格中的值

时间:2017-09-04 08:50:45

标签: r

我从facebook api fbRads提取数据。以下是我拥有的数据框样本:

mydata <-  fread('ID,ACTIONS
      02,"list(action_type = c("link_click", "post_reaction", "page_engagement", "post_engagement"), value = c("1", "4", "5", "5"))"
      03,"list(action_type = c("app_custom_event.fb_mobile_activate_app", "app_custom_event.fb_mobile_add_to_cart", "app_custom_event.fb_mobile_content_view", "app_custom_event.fb_mobile_purchase", "app_custom_event.fb_mobile_search", "app_custom_event.other", "like", "link_click", "mobile_app_install", "offsite_conversion.fb_pixel_add_to_cart", "offsite_conversion.fb_pixel_add_to_wishlist", "offsite_conversion.fb_pixel_lead", "offsite_conversion.fb_pixel_purchase", "offsite_conversion.fb_pixel_search", "offsite_conversion.fb_pixel_view_content", "post_reaction", "page_engagement", "post_engagement", "offsite_conversion", "app_custom_event"), value = c("994", "219", "1696", "9", "47", "425", "67", "2267", "37", "348", "53", "3", "7", "218", "3286", "145", "2479", "2412", "3915", "3390"))"
      04,"NULL"
      05,"list(action_type = c("app_custom_event.fb_mobile_activate_app", "app_custom_event.fb_mobile_add_to_cart", "app_custom_event.fb_mobile_content_view", "app_custom_event.fb_mobile_purchase", "app_custom_event.fb_mobile_search", "app_custom_event.other", "like", "link_click", "mobile_app_install", "offsite_conversion.fb_pixel_add_to_cart", "offsite_conversion.fb_pixel_add_to_wishlist", "offsite_conversion.fb_pixel_lead", "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", "app_custom_event"), value = c("1703", "188", "2233", "13", "155", "731", "229", "2568", "62", "303", "46", "7", "17", "257", "4433", "1", "473", "3271", "3042", "5063", "5023"))"')

我需要针对每个ID查找app_custom_event.fb_mobile_purchase的值。 ACTION列在每个单元格中包含两个列表,即action_typevalue 我期待的输出是:

mydata <-  fread('ID,app_custom_event.fb_mobile_purchase
      02,"NULL"
      03,"9"
      04,"NULL"
      05,"13"')

我是否需要使用词典来获取值?任何方法都将受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

这是一个开始:

lapply(mydata$ACTIONS, function(i){
  x <- eval(parse(text = i))
  ix <- which(x$action_type == "app_custom_event.fb_mobile_purchase")
  x$value[ ix ]
})

我不知道 fbRads 包,但它必须有一些“读取”功能,以避免这个问题。