2017-10-25 6 views
4

이 같은 패키지 fbRads에서 fb_insights 사용하고 있습니다 함께 (내 진짜 문제에 더 많은 통계를 사용)목록이 포함 된 열을 얻기 및 R에있는 다른 사람과 병합 - fbRads

fb_campaigns <- rbindlist(lapply(l, function(l) cbind(Campaign = l$campaign_name, rbindlist(l$actions))))

아, 그리고 몇 가지 경고 (내가 뭔가 잘못하고있어 알고 있지만 그것을 해결할 수없는) 수 :

Warning messages: 1: In data.table::data.table(...) : Item 1 is of size 11 but maximum size is 104 (recycled leaving remainder of 5 items)

결과는 내가 필요로하는 모든 데이터와 데이터 프레임을 (캠페인, action_type, value)하지만 ... th e "action_types"가있는 열과 그 번호가 잘못되었습니다. 액션 데이터는 행의 캠페인에서 발생한 것 같지 않습니다.

캠페인과 작업 유형을 병합하려면 어떻게해야합니까?

올바른 행의 데이터를 만든 후에는 reshape을 사용하여 값이있는 action_types 열을 만듭니다.

가 데이터 I는 FB의 RADS에서 얻을 내가 변환 할 것은 다음과 같이 있습니다 : enter image description here

내 코드를 사용하여 얻을 데이터 (형식은 값의 순서 OK입니다,하지만이처럼, 그들은 캠페인이 아닌 값)

enter image description here

답변

0

daroczig 나에게 솔루션 우는 소리를주고, 잘 작동하는 것 같다 있습니다!

## list of action types to extract 
    actiontypes <- c('link_click', 'comment', 'like', 'post') 

## extract actions from the data returned by the Insights API 
    lactions <- unlist(lapply(l, function(x) x$actions), recursive = FALSE) 

## extract fields from the actions 
    library(data.table) 
    lactions <- rbindlist(lapply(lactions, function(actions) { 
     setnames(as.data.table(
      do.call(cbind, 
        lapply(actiontypes, 
          function(action) { 
           if (is.null(actions)) return(0) 
           value <- subset(actions, action_type == action, value) 
           if (nrow(value) == 0) return(0) else 

    return(value[[1]]) 
           }))), 
       actiontypes) 
      })) 

## Merging the dataframe with the original data and the dataframe with the actions 
    fb_campaigns <- cbind(l[,c(1,4:11)],lactions))