Combining columns in R based on matching beginnings of column title names

后端 未结 3 1777
孤独总比滥情好
孤独总比滥情好 2021-01-25 13:08

I have a dataframe that looks somewhat like the following. A1U_sweet is actually the 19th column in the real dataframe, and C1U_sweet is the 39th column in the real dataframe. T

3条回答
  •  余生分开走
    2021-01-25 13:40

    Consider mapply to compare A columns and C columns elementwise and assign all B columns at once. And use sub which unlike gsub, sub only replaces first occurrence in case there are A's elsewhere in column header.

    new_B_cols <- sub("A", "B", names(df)[grep("^A", names(df))])
    
    replace_na <- function(aa, cc) {
         aa[is.na(aa)] <- cc[is.na(aa)]
         return(aa) 
    }
    
    df[new_B_cols] <- mapply(replace_na, df[grep("^A", names(df))], df[grep("^C", names(df))])
    
    df[order(names(df))]
    #   A1U_sweet A2F_dip A3U_bbq B1U_sweet B2F_dip B3U_bbq C1U_sweet C2F_dip C3U_bbq
    # 1         1       2       1         1       2       1        NA      NA      NA
    # 2        NA      NA      NA         4       1       2         4       1       2
    # 3         2       4       7         2       4       7        NA      NA      NA
    

提交回复
热议问题