Don't use loops in R
Fusing arrays 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 160
AX-3 160 120 NA'
Fus <- read.table(text=Fus, header=T)
Some idea to perform this fusion?
• 2,935 views
•
link
2 answers
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
• 0 views
•
link
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.
• 0 views
•
link
• 0 views
•
link
That's true :-)
My point was to give guidance rather than ready-to-use code.
• 0 views
•
link
Log in to answer this question.