Reinsert Na into vector

I have a value vector containing Na These values need to be processed by an external program that cannot process Na, so they are stripped, written to a file, processed, and then read back to produce a vector of non Na quantity length For example, if the input is 734 Na 546 Na 1 Na, the output will only be 7 values What I need to do is reinsert the Na into place

Therefore, two vectors X and y are given:

> X
 [1]  64   1   9 100  16  NA  25  NA   4  49  36  NA  81
 > Y
 [1]  8  1  3 10  4  5  2  7  6  9

Production:

8 1 3 10 4 NA 5 NA 2 7 6 NA 9

(you may notice that x is y ^ 2, which is just an example)

I can type out a function to do this, but I wonder if there is any good cunning way to do this... Split, list, length... Um

Solution

Another variation of the same subject

rena <- function(X,Z){
    X[which(!is.na(X))]=Z    
    X
}

R automatically fills the rest with Na

Editor: Marek revised

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>