'replacement has length zero' error in R

Donna

I'm trying to impute NA's of temperature data in R. It is spatiotemporal data which has 487 observatories and 60 time units (60 months). What I want to do here is replace NA with the value of which has the smallest distance (not zero) from the NA's observatory in the same month.

Here is my R code (temp_1 is the name of my data).

pos.min = function(v){        # find positive minimum index
  v.na = v
  v.na[v==0] = NA
  return(which.min(v.na))
}


for (i in 1:60){
 for (j in 1:sum(is.na(temp_1[i,]))){
   na.index=which(is.na(temp_1[i,]))
   dz.index=pos.min(dz[na.index[j],])
   new=temp_1[i,dz.index]
   temp_1[i,][is.na(temp_1[i,])][j]=new
   }
}

However, when I run this I get an error message

Error in temp_1[i, ][is.na(temp_1[i, ])][j] = new : replacement has length zero

I typed class(new) and it says data.frame, so I've changed it into numeric by new=as.numeric(temp_1[i,dz.inex]). But it comes to the same error.

I don't understand why I get this error message... I appreciate your help a lot.

Parfait

Consider sapply() to traverse the columns and maintain the two-dimensionsal structure. Below example dataset demonstrates of converting NAs to nonzero minimums of corresponding column:

df <- read.table(text="OBS  MONTH1  MONTH2  MONTH3  MONTH4  MONTH5  MONTH6
              1 0.306001774     0.086538253 0.9847485   0.920696749 0.806839772 0.693047683
              2  0.795098073             NA 0.08102032  0.473177189 0.852177898          NA
              3  0.205973354    0.902099959 0.914812457          NA 0.608290972 0.378916134
              4           NA    0.000372107 0.350874402 0.915298814 0.817865272 0.225742663
              5  0.478680167    0.812487579 0.630341993 0.235519315 0.694856788 0.181300605
              6  0.913115578    0.018699114 0.104682383 0.871933902 0.051088907 0.731334073
              7  0.639176591    0.177650025 0.180534357          NA 0.296920889 0.869592176
              8  0.458452966    0.439206666          NA 0.887944511 0.071936749 0.304492684
              9  0.218429871    0.639603625 0.134885823 0.113512933          NA 0.472305502
              10 0.027337984             NA  0.37154713 0.400568794 0.928564041 0.559873876
                 ", header=TRUE)

newdf <- data.frame(sapply(df, function(col) {
     ifelse(is.na(col) & min(col[!is.na(col)]) > 0, min(col[!is.na(col)]), col)
}))
newdf

#    OBS     MONTH1      MONTH2     MONTH3    MONTH4     MONTH5    MONTH6
# 1    1 0.30600177 0.086538253 0.98474850 0.9206967 0.80683977 0.6930477
# 2    2 0.79509807 0.000372107 0.08102032 0.4731772 0.85217790 0.1813006
# 3    3 0.20597335 0.902099959 0.91481246 0.1135129 0.60829097 0.3789161
# 4    4 0.02733798 0.000372107 0.35087440 0.9152988 0.81786527 0.2257427
# 5    5 0.47868017 0.812487579 0.63034199 0.2355193 0.69485679 0.1813006
# 6    6 0.91311558 0.018699114 0.10468238 0.8719339 0.05108891 0.7313341
# 7    7 0.63917659 0.177650025 0.18053436 0.1135129 0.29692089 0.8695922
# 8    8 0.45845297 0.439206666 0.08102032 0.8879445 0.07193675 0.3044927
# 9    9 0.21842987 0.639603625 0.13488582 0.1135129 0.05108891 0.4723055
# 10  10 0.02733798 0.000372107 0.37154713 0.4005688 0.92856404 0.5598739

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R error in '[<-.data.frame'... replacement has # items, need #

From Dev

Error in boot() related to replacement length and data or data types? - R

From Dev

R number of items to replace is not a multiple of replacement length

From Dev

replacement has length zero in nested loop

From Dev

R error: "number of items to replace is not a multiple of replacement length"

From Dev

R caret package rfe never finishes error task 1 failed - "replacement has length zero"

From Dev

While loop in R giving "argument is of length zero" error

From Dev

R Error: replacement diagonal has wrong length

From Dev

R aggregate error: "replacement has <foo> rows, data has <bar>"

From Dev

R - argument is of length zero in if statement

From Dev

for Loop - Replacement has length zero

From Dev

R “argument is of length zero” in if(condition)

From Dev

error message in R : if (nomZ %in% coded) { : argument is of length zero

From Dev

R caret package rfe never finishes error task 1 failed - "replacement has length zero"

From Dev

R Error: replacement diagonal has wrong length

From Dev

Error: condition has length > 1 and only the first element will be used in r

From Dev

Error: Replacement has length zero R

From Dev

error message in R : if (nomZ %in% coded) { : argument is of length zero

From Dev

"Argument of length zero" in R

From Dev

R pca ggbiplot error : replacement has 36 rows, data has 35

From Dev

Error in R :Number of items to replace is not a multiple of replacement length

From Dev

if statement in R. Error argument is of length zero

From Dev

R Quantile Error - replacement has n rows, data has p

From Dev

r if statement meet error: argument is of length zero

From Dev

TSLM Error: Replacement Has Length Zero

From Dev

Why for loop is working with one data set, yet error of replacement has length zero with very similar data set r

From Dev

Error number of items to replace is not a multiple of replacement length

From Dev

String cannot be of zero length error

From Dev

Error in fix_replacement(replacement) in R

Related Related

  1. 1

    R error in '[<-.data.frame'... replacement has # items, need #

  2. 2

    Error in boot() related to replacement length and data or data types? - R

  3. 3

    R number of items to replace is not a multiple of replacement length

  4. 4

    replacement has length zero in nested loop

  5. 5

    R error: "number of items to replace is not a multiple of replacement length"

  6. 6

    R caret package rfe never finishes error task 1 failed - "replacement has length zero"

  7. 7

    While loop in R giving "argument is of length zero" error

  8. 8

    R Error: replacement diagonal has wrong length

  9. 9

    R aggregate error: "replacement has <foo> rows, data has <bar>"

  10. 10

    R - argument is of length zero in if statement

  11. 11

    for Loop - Replacement has length zero

  12. 12

    R “argument is of length zero” in if(condition)

  13. 13

    error message in R : if (nomZ %in% coded) { : argument is of length zero

  14. 14

    R caret package rfe never finishes error task 1 failed - "replacement has length zero"

  15. 15

    R Error: replacement diagonal has wrong length

  16. 16

    Error: condition has length > 1 and only the first element will be used in r

  17. 17

    Error: Replacement has length zero R

  18. 18

    error message in R : if (nomZ %in% coded) { : argument is of length zero

  19. 19

    "Argument of length zero" in R

  20. 20

    R pca ggbiplot error : replacement has 36 rows, data has 35

  21. 21

    Error in R :Number of items to replace is not a multiple of replacement length

  22. 22

    if statement in R. Error argument is of length zero

  23. 23

    R Quantile Error - replacement has n rows, data has p

  24. 24

    r if statement meet error: argument is of length zero

  25. 25

    TSLM Error: Replacement Has Length Zero

  26. 26

    Why for loop is working with one data set, yet error of replacement has length zero with very similar data set r

  27. 27

    Error number of items to replace is not a multiple of replacement length

  28. 28

    String cannot be of zero length error

  29. 29

    Error in fix_replacement(replacement) in R

HotTag

Archive