2017-06-30 81 views
0

我試圖找出如何多列粘貼在一起使用一些分離器,但我希望將列在假設2。例如羣結合,我有一個數據幀df如下:如何粘貼柱分批

df <- data.frame(matrix(ncol = 4, nrow = 3)) 
x <- c("a", "b", "c", "d") 
colnames(df) <- x 
df$a <- c("man", "bear", "pig") 
df$b <- c("chicken", "moose", "bear") 
df$c <- c("fish", "dog", "bear") 
df$d <- c("dog", "mouse", "moose") 

df 
#  a  b c  d 
#1 man chicken fish dog 
#2 bear moose dog mouse 
#3 pig bear bear moose 

,我想列a + bc + d,分別結合。我可以得到它通過逐步將其粘貼在一起,這樣的工作:

df$combined1 <- paste(df$a, df$b, sep = " + ") 
df$combined2 <- paste(df$c, df$d, sep = " + ") 

,但我真的想堅持DRY原則的一些更清晰的代碼。我試着用unite做的很好,但沒有運氣。有什麼想法嗎?

感謝您的幫助!

回答

2

首先,在可讀性的精神讓我們簡化您的數據創建的代碼。當然並不需要所有這些中間變量:

df <- data.frame(
    a = c("man", "bear", "pig"), 
    b = c("chicken", "moose", "bear"), 
    c = c("fish", "dog", "bear"), 
    d = c("dog", "mouse", "moose") 
) 

現在你的問題的肉。這是非常一般的。首先我們定義要組合的列表,然後合併它們,創建組合列的名稱,並僅通過參考數據和組合列表將它們組合起來:

cols_to_combine = list(c(1, 2), c(3, 4)) 

for (comb in cols_to_combine) { 
    df[[paste0("combined_", paste(comb, collapse = "_"))]] = 
     do.call(paste, args = c(df[comb], sep = " + ")) 
} 

df 
#  a  b c  d combined_1_2 combined_3_4 
# 1 man chicken fish dog man + chicken fish + dog 
# 2 bear moose dog mouse bear + moose dog + mouse 
# 3 pig bear bear moose pig + bear bear + moose