Вопрос

I want to replace 0 (missing data) with data from another array.

Both data has 2013~1995 January ice sheet data, and I want to analyze temperature data only (in column 3), 4464 temperatures data.

Partial data looks like this: This is from ErinJan[,,1]

            [,1] [,2]  [,3]  [,4]  [,5] [,6]  [,7]  [,8]
    [1   ,]   21   10   0.0   0.0  0.0    0  0.0    0
    [2   ,]   21   11   0.0   0.0  0.0    0  0.0    0
    [3   ,]   21   12 -16.3 867.4  0.0    0  0.0    0
    [4   ,]   21   13 -16.4 867.5  6.9   81 63.2    0
    [5   ,]   21   14 -16.5 867.2  7.6   83 63.0    0
    [6   ,]   21   15 -16.5 867.1  7.9   84 63.0    0
    [7   ,]   21   16   0.0 867.1  8.0   86 62.0    0
    [8   ,]   21   17 -16.3 867.0  8.4   87 62.0    0
    [9   ,]   21   18   0.0 866.9  8.3   85 62.0    0

And HarryJan[,,1] looks like:

            [,1] [,2]  [,3]  [,4]  [,5] [,6]  [,7]  [,8]
    [1   ,]   21   10 -12.4 877.6   2.5   52 444.0   9.1
    [2   ,]   21   11 -12.6 877.6   3.0   55 444.0   9.1
    [3   ,]   21   12 -12.9 877.5   3.8   52 444.0   9.1
    [4   ,]   21   13 -13.0 877.5   3.6   53 444.0   9.1
    [5   ,]   21   14 -12.9 877.3   3.3   51 444.0   9.1
    [6   ,]   21   15 -13.0 877.3   4.1   53 444.0   9.1
    [7   ,]   21   16 -13.2 877.1   3.6   53 444.0   9.1
    [8   ,]   21   17 -13.4 877.3   3.3   45 444.0   9.1
    [9   ,]   21   18 -13.4 877.3   3.6   48 444.0   9.1

Both array has the same format and same type of data like above. My goal is replace ErinJan[,3,1] missing data with HarryJan[,3,1] and so on. For example, ErinJan[1,3,1] is missing data, so I need to replace with HarryJan[1,3,1]. (0 will be replaced with -12.4.)

If I can code something like this:

   for (i in 1: 19){
        if ErinJan[,3,i] == 0 {
            replace value using HarryJan
        }
        else {
            do nothing
        }
     }

Is this possible? I am not sure what command I should use. Replace?

I'd appreciate your help.

Это было полезно?

Решение

If I understand your question, then this:

ErinJan[ErinJan == 0] <- HarryJan[ErinJan == 0]

would make the replacement across the whole matrix. I am not sure how your columns are arranged, but if you pull out all the column 3s, you should be able to do the same replacement for just that variable.

I believe this will work for column 3 only:

ErinJan[,3,][ErinJan[,3,]==0] = HarryJan[,3,][ErinJan[,3,]==0]

This works by indexing just the elements of the first array that equal zero and using those same indices to pull values from the second array.

If you find the stuff in square brackets confusing, here is another way to break it out in two steps:

JanInd = which(ErinJan[,3,]==0)
ErinJan[,3,][JanInd] = HarryJan[,3,][JanInd]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top