Search code examples
rnafusion

Fusing dfs with equal dimmensions


I would like to fuse 3 data frames (AA, AB and BB) which contain exactly the same dimensions and will never contain a number in the same coordinate (if one contain a number the others will contain a NA. Can also be true that a specific coordinate contain NA for all data frames). This is my input:

AA <- 'pr_id  sample1  sample2 sample3
            AX-1   NA       120     130    
            AX-2   NA       NA     NA
            AX-3   NA       NA     NA'
AA <- read.table(text=AA, header=T)

AB <- 'pr_id  sample1  sample2 sample3
            AX-1   100       NA     NA    
            AX-2   NA       180     NA
            AX-3   NA       120     NA'
AB <- read.table(text=AB, header=T)

BB <- 'pr_id  sample1  sample2 sample3
            AX-1   NA       NA     NA    
            AX-2   150       NA     NA
            AX-3   160       NA     NA'
BB <- read.table(text=BB, header=T) 

My expected output:

Fus <- 'pr_id  sample1  sample2 sample3
            AX-1   100       120     130    
            AX-2   150       180     NA
            AX-3   160       120     NA'
Fus <- read.table(text=Fus, header=T)

Some idea to perform this fusion?


Solution

  • Here is a possible solution giving you a matrix

    L <- lapply(list(AA, AB, BB), function(x) { row.names(x) <- x[[1]]; as.matrix(x[-1])})
    Reduce(function(x, y) ifelse(is.na(x), y, x), L)
    

    If you want a dataframe:

    L <- lapply(list(AA, AB, BB), function(x) { row.names(x) <- x[[1]]; as.matrix(x[-1])})
    X <- Reduce(function(x, y) ifelse(is.na(x), y, x), L)
    as.data.frame(X)  # Fus <- as.data.frame(X)
    

    You can do it also in a loop:

    L <- lapply(list(AA, AB, BB), function(x) { row.names(x) <- x[[1]]; as.matrix(x[-1])})
    X <- L[[1]]
    for (i in 2:length(L)) X <- ifelse(is.na(X), L[[i]], X)
    X
    

    or

    L <- lapply(list(AA, AB, BB), function(x) { row.names(x) <- x[[1]]; as.matrix(x[-1])})
    X <- L[[1]]
    for (i in 2:length(L)) X[is.na(X)] <- L[[i]][is.na(X)]
    X