2017-09-04 133 views
2

我已经从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"))"') 

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

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

我需要用字典来获取价值?任何方法将不胜感激。

回答

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包,但是它必须有一些“读”功能,来避免这个问题。

+0

非常感谢。 我已经通过这个包的文档。没有这样的读取功能可以给我表格格式的数据 –