R/create_union_mad.R

Defines functions rowSums.na create.union.mad

Documented in create.union.mad

#' @export

create.union.mad <- function(channel, export, config) {
  print(channel)
  
  output <- create.union.aggregated(channel, export)
  
  output <- data.frame(output)
  
  if (dim(output)[1] > 0) {
    
    config.columns <- unlist(sapply(config[[paste(tolower(channel), "engagements", "columns", sep = ".")]], 
                                    function(x) if (x %in% names(output)) {x}))
    
    for (column in config.columns) {
      output[, column] <- as.numeric(output[, column])
    }
    
    output$Total.Engagements <- rowSums.na(output, config.columns, "Total.Engagements")
      
    
    names(output) <- gsub('FB.Views', 'Video.Starts', names(output), fixed = TRUE)
    names(output) <- make.names(gsub(paste0("(",channel, ")\\."), "", names(output)))
    names(output) <- gsub('Comments', 'Replies', names(output), fixed = TRUE)
    names(output) <- gsub('Retweets', 'Shares', names(output), fixed = TRUE)
    names(output) <- gsub('Url.Clicks', 'Link.Clicks', names(output), fixed = TRUE)
    
    for (column in names(output)) {
      if (length(grep("X[[:digit:]]", column)) > 0) {
        promoted.column <- paste('Promoted', substring(column, 2), sep = '.')
      } else {
        promoted.column <- paste('Promoted', column, sep = '.')
      }
      if (promoted.column %in% names(output) && !(column == 'Engagements')) {
        output[, column] <- as.numeric(output[, column])
        output[, promoted.column] <- as.numeric(output[, promoted.column])
        output[, column] <- rowSums.na(output, c(column, promoted.column), column)
      }
    }
    
    
    output$Total.Reach.Impressions. <- NA
    
    if ('Impressions.Paid' %in% names(output)) {
      output$Impressions.Paid <- as.numeric(output$Impressions.Paid)
      output$Total.Reach.Impressions. <- rowSums.na(output, c('Total.Reach.Impressions.', 'Impressions.Paid'), 'Total.Reach.Impressions.')
    }
    
    if ('Impressions.Unpaid' %in% names(output)) {
      output$Impressions.Unpaid <- as.numeric(output$Impressions.Unpaid)
      output$Total.Reach.Impressions. <- rowSums.na(output, c('Total.Reach.Impressions.', 'Impressions.Unpaid'), 'Total.Reach.Impressions.')
    }
    
    if ('Impressions' %in% names(output)) {
      output$Impressions <- as.numeric(output$Impressions)
      output$Total.Reach.Impressions. <- rowSums.na(output, c('Total.Reach.Impressions.', 'Impressions'),'Total.Reach.Impressions.')
    }

    #output$Total.Engagements.Explido..Paid. <- rowSums(output[, config.columns], na.rm = TRUE)
    print(names(output))
    
    output.columns <- unlist(sapply(c('Shares', 'Replies', 'Likes', 'Link.Clicks', 'X30.sec.views'), 
                                    function(x) if (x %in% names(output)) {x}))
    for (column in output.columns) {
      output[, column] <- as.numeric(output[, column])
    }
    
    
    output$Other.Engagements <- output$Total.Engagements - rowSums(output[,output.columns], na.rm = TRUE)
    
  }
  
  output
  
}

rowSums.na <- function(x, columns, new.column) {
  if (!(new.column %in% names(x))) {
    x[, new.column] <- NA
  }
  x[which(apply(x[, columns], 1, function(c) any(!(is.na(c))))), new.column] <- 
    rowSums(x[which(apply(x[, columns], 1, function(c) any(!(is.na(c))))), columns], na.rm = TRUE)
  x[, new.column]
}
omelyanchikd/merger documentation built on July 13, 2017, 6:24 p.m.