Fusing arrays with equal dimmensions
2
0
Entering edit mode
6.4 years ago
viniciushs.z ▴ 20

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     160
                AX-3   160       120     NA'
    Fus <- read.table(text=Fus, header=T)

Some idea to perform this fusion?

r array • 1.5k views
ADD COMMENT
3
Entering edit mode
6.4 years ago

Something like this maybe:

FUS <- AA;
to_replace<-is.na(FUS); # indices of NAs in FUS/AA
FUS[to_replace]<-AB[to_replace]; # replace NAs in AA by AB values
to_replace<-is.na(FUS); # remaining NAs
FUS[to_replace]<-BB[to_replace]; # replace NAs in FUS by BB values
ADD COMMENT
1
Entering edit mode
6.4 years ago
rioualen ▴ 610

If you are sure they'll always have the same dimension and only one numeric value, just create an empty table, loop columns and rows in your tables and fill the empty one.

You can use is.na() function to test each position.

ADD COMMENT
0
Entering edit mode

Don't use loops in R

ADD REPLY
0
Entering edit mode

That's true :-)

My point was to give guidance rather than ready-to-use code.

ADD REPLY

Login before adding your answer.

Traffic: 1973 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6