2017-02-16 26 views
0

我想創建一個dplyr::bind_rows suped式版本,避免Unequal factor levels: coercing to character警告,當係數列存在於我們試圖結合DFS(也可能產生非要素列)。這裏有一個例子:綁定行與一些因素列

df1 <- dplyr::data_frame(age = 1:3, gender = factor(c("male", "female", "female")), district = factor(c("north", "south", "west"))) 
df2 <- dplyr::data_frame(age = 4:6, gender = factor(c("male", "neutral", "neutral")), district = factor(c("central", "north", "east"))) 

然後bind_rows_with_factor_columns(df1, df2)收益(不含警告):

dplyr::data_frame(
    age = 1:6, 
    gender = factor(c("male", "female", "female", "male", "neutral", "neutral")), 
    district = factor(c("north", "south", "west", "central", "north", "east")) 
) 

這是我到目前爲止有:

bind_rows_with_factor_columns <- function(...) { 
    factor_columns <- purrr::map(..., function(df) { 
     colnames(dplyr::select_if(df, is.factor)) 
    }) 

    if (length(unique(factor_columns)) > 1) { 
     stop("All factor columns in dfs must have the same column names") 
    } 

    df_list <- purrr::map(..., function (df) { 
    purrr::map_if(df, is.factor, as.character) %>% dplyr::as_data_frame() 
    }) 

    dplyr::bind_rows(df_list) %>% 
    purrr::map_at(factor_columns[[1]], as.factor) %>% 
    dplyr::as_data_frame() 
} 

我想知道如果任何人有任何想法如何整合forcats包來避免將因素強加給角色,或者如果有人總體上有任何建議來提升此效果保持相同的功能(我想堅持tidyverse語法)。謝謝!

+0

爲什麼不'do.call(rbind,列表(DF1,DF2))'? – Sotos

+0

'suppressWarnings'或'purrr :: quietly'? – Axeman

回答

0

展望基於從朋友很好的解決方案來回答我的問題:

bind_rows_with_factor_columns <- function(...) { 
    purrr::pmap_df(list(...), function(...) { 
    cols_to_bind <- list(...) 
    if (all(purrr::map_lgl(cols_to_bind, is.factor))) { 
     forcats::fct_c(cols_to_bind) 
    } else { 
     unlist(cols_to_bind) 
    } 
    }) 
}