Search code examples
r

Need a way of clumping columns together without trailing NAs and reversing the result


I am looking for a way of creating a new column composed of a set columns (bin 1-9 in this example) from an existing data frame without trailing NAs and the results reversed. All this without using loops. Thanks for your help!

data set example:

id bin_1 bin_2 bin_3 bin_4 bin_5 bin_6 bin_7 bin_8 bin_9
1   a     b     c     d     e     na    na    na    na
2   a     b     na    na    na    na    na    na    na

ideal result:

id reversed_noNAs
1      edcba
2      ba

I normally combine fields using do.call/paste but this clumps in the NAs:

do.call(paste, c(df[c("bin_1", "bin_2", "bin_3", "bin_4", "bin_5", "bin_6","bin_7","bin_8","bin_9")], sep = ""))

thanks!


Solution

  • Assuming your missing values are coded NA and not the string "NA", you could do:

    apply(
        X=df[, grepl('^bin_\\d+$', names(df))], 
        MARGIN=1, 
        FUN=function(x) {paste(rev(na.omit(x)), collapse='')}
    )
    

    If your missing values are coded as strings, you could use sub instead of na.omit:

    apply(
        X=df[, grepl('^bin_\\d+$', names(df))], 
        MARGIN=1, 
        FUN=function(x) {paste(rev(sub('^NA$', '', x)), collapse='')}
    )