dimanche 24 décembre 2017

R help : using IF statement in matrix but Error. 'data' must be of a vector type, was 'NULL'

for(i in 1:15){
  for(j in 1:nrow(all_psr)){
        for(k in 2:7){
      yesorno <- Reduce(intersect, list(all_psr[3*i+3][j], rest_deter[k]))
      if(yesorno != 0){all_psr[3*i+4][j] <- k
      }  else{
           all_psr[3*i+4][j] <- NULL
       }
      }
  }
}

Hi. I'd like to use these for and if statements for making a judgment about which restrictiveness index a certain code has. the method is to check if a certain element in lists of restrictiveness index is identical with the value I want to judge. If the result is true, the restrictiveness index of the value is assigned. if the result is false, the program does same thing with other lists of restrictiveness index.

dataframe "all_psr" is so big to upload on this page so I cut into small one, which is dataframe "k". when I run the code above R displays the error message like this. all_psr consists of 15 countries' data.

Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : 'data' must be of a vector type, was 'NULL'

one thing to refer : there are some blanks in the dataframe "all_psr".

How should I handle this problem?

> dput(rest_deter)
 structure(list(V1 = c(1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
>     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
>     NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), V2 = structure(c(2L, 
>     3L, 5L, 4L, 6L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 1L, 1L, 
>     16L, 17L, 1L, 1L, 1L, 1L, 7L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
>     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "2", 
>     "CTSH", "CTSH or CTH+RVC<50", "CTSH or RVC(35)", "CTSH or RVC(40)", 
>     "CTSH or RVC(45)", "CTSH or RVC<=50", "CTSH or RVC<50", "CTSH or RVC>=50", 
>     "CTSH+ECTC", "CTSH+ECTC or CTSH+RVC(45)", "CTSH+ECTC or CTSH+RVC(50)", 
>     "CTSH+ECTC or CTSH+RVC<50", "CTSH+ECTC or RVC(40)", "CTSH+ECTC or RVC<50", 
>     "RM(0)"), class = "factor"), V3 = structure(c(2L, 3L, 5L, 4L, 
>     6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
>     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
>     1L, 1L, 1L, 1L), .Label = c("", "3", "CTSH+CM>50 or RVC(40)", 
>     "CTSH+ECTC+RVC<=50", "CTSH+RVC(45)", "CTSH+RVC<=50"), class = "factor"), 
>         V4 = structure(c(1L, 3L, 8L, 37L, 9L, 12L, 14L, 15L, 16L, 
>         17L, 18L, 19L, 36L, 34L, 25L, 20L, 21L, 22L, 27L, 39L, 40L, 
>         30L, 33L, 32L, 26L, 35L, 23L, 31L, 13L, 38L, 24L, 7L, 4L, 
>         6L, 28L, 5L, 11L, 10L, 29L, 2L), .Label = c("4", "CC or RVC(40)", 
>         "CTH", "CTH or CM>50", "CTH or CTH+CM>50", "CTH or CTH+ECTC+CM<50", 
>         "CTH or CTH+ECTC+CM>50", "CTH or CTH+RVC(45)", "CTH or CTH+RVC<=50", 
>         "CTH or CTSH+CM>=50", "CTH or CTSH+CM>50", "CTH or CTSH+ECTC+RVC>=50", 
>         "CTH or CTSH+RVC(40)", "CTH or CTSH+RVC(45)", "CTH or CTSH+RVC<=50", 
>         "CTH or CTSH+RVC<50", "CTH or CTSH+RVC>=50", "CTH or NC(50)", 
>         "CTH or NC(50)+TECH", "CTH or NC<50", "CTH or NC>50", "CTH or RVC(40)", 
>         "CTH or RVC=<40", "CTH or TECH or CM<50", "CTH+ECTC or CM<50 or TECH", 
>         "CTH+ECTC or CM>=50 or TECH", "CTH+ECTC or CTH or RVC<=50", 
>         "CTH+ECTC or CTH+ECTC+CM>50", "CTH+ECTC or CTSH+CM<50", "CTH+ECTC or CTSH+RVC<=50", 
>         "CTH+ECTC or RVC(40)", "CTH+ECTC or RVC(45)", "CTH+ECTC or RVC<=50", 
>         "CTH+ECTC or RVC<50", "CTH+ECTC or TECH or CM<50", "CTH+TECH or RVC<50", 
>         "CTSH+CM>=50 or RVC<50", "RVC(40)", "RVC<=50", "RVC<50"), class = "factor"), 
>         V5 = structure(c(2L, 9L, 3L, 33L, 8L, 10L, 14L, 15L, 20L, 
>         21L, 22L, 23L, 37L, 36L, 39L, 24L, 26L, 27L, 28L, 34L, 35L, 
>         29L, 32L, 31L, 13L, 6L, 17L, 25L, 16L, 18L, 12L, 38L, 7L, 
>         30L, 19L, 5L, 11L, 4L, 1L, 1L), .Label = c("", "5", "CC or RVC<=50", 
>         "CC or RVC<50", "CC+TECH or RVC(40)", "CM>=50", "CTH or CTSH+TECH", 
>         "CTH or RVC(45)", "CTH or RVC<=50", "CTH or RVC<50", "CTH or TECH or RVC(40)", 
>         "CTH or TECH+RVC>=50", "CTH+CM>=50", "CTH+CM>=50 or RVC<50", 
>         "CTH+ECTC", "CTH+ECTC or CM<50", "CTH+ECTC or CM>=50", "CTH+ECTC or CM>50", 
>         "CTH+ECTC or CTH+CM>50", "CTH+ECTC or CTH+RVC(50)", "CTH+ECTC or CTH+RVC<50", 
>         "CTH+ECTC or CTSH+ECTC", "CTH+ECTC or NC(50)", "CTH+ECTC or NC<50", 
>         "CTH+ECTC or TECH", "CTH+ECTC+RVC<=50", "CTH+ECTC+RVC>50 or CTSH", 
>         "CTH+NC(50)", "CTH+NC>50", "CTH+RVC(40)", "CTH+RVC(45)", 
>         "CTH+RVC<50", "NC(45)", "NC(50)", "NC<50", "RVC>=50", "RVC>50", 
>         "TECH or CTH+ECTC", "WO or RVC<50"), class = "factor"), V6 = structure(c(2L, 
>         3L, 4L, 34L, 5L, 6L, 9L, 10L, 11L, 13L, 15L, 16L, 22L, 1L, 
>         23L, 17L, 18L, 19L, 20L, 35L, 36L, 30L, 32L, 31L, 27L, 26L, 
>         29L, 28L, 25L, 24L, 12L, 7L, 8L, 14L, 33L, 21L, 1L, 1L, 1L, 
>         1L), .Label = c("", "6", "CC", "CC or CC+RVC(40)", "CC or CC+RVC(45)", 
>         "CC or CC+RVC<50", "CC or CM>50", "CC or CTH+CM<50", "CC or CTH+RVC<=50", 
>         "CC or CTH+RVC<50", "CC or CTH+TECH", "CC or ECTC+TECH", 
>         "CC or NC(50)+TECH", "CC or RVC(60)", "CC or TECH", "CC or TECH+ECTC", 
>         "CC+ECTC", "CC+ECTC or CTH+RVC>=50", "CC+TECH or NC(50)", 
>         "CM<50", "CTH and RVC(50)", "CTH+CM<=50", "CTH+CM<50", "CTH+CM<50 or CM<50", 
>         "CTH+CM<50 or CM>=50", "CTH+ECTC+CM<=50", "CTH+ECTC+CM<50", 
>         "CTH+ECTC+CM<50 or CM>=50", "CTH+ECTC+CM>=50", "CTH+NC(40)", 
>         "CTH+NC(45)", "CTH+NC<50 or NC<50", "CTH+RVC(60)", "CTH+RVC>=50", 
>         "CTH+RVC>50", "CTH+TECH or CC"), class = "factor"), V7 = structure(c(2L, 
>         9L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 
>         16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 1L, 1L, 
>         1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
>         "7", "CC+CM(0)", "CC+CM<=50", "CC+CM<50", "CC+ECTC or CC+RVC(45)", 
>         "CC+ECTC+CM(0)", "CC+ECTC+NC(50)", "CC+ECTC+TECH", "CC+NC(50)", 
>         "CC+NC>50", "CC+RVC(45)", "CC+TECH", "CM(0)", "CTH+ECTC+CM<30", 
>         "CTH+ECTC+CM<50 or TECH", "CTH+RM(50)", "CTH+TECH", "CTSH+RM(50)", 
>         "TECH", "TECH+CM<=50", "TECH+CM<50", "WO", "WO+CM<50", "WO+NC>=50"
>         ), class = "factor")), .Names = c("V1", "V2", "V3", "V4", 
>     "V5", "V6", "V7"), class = "data.frame", row.names = c(NA, -40L
>     ))





dput(k)
    structure(list(V1 = structure(c(11L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L), .Label = c("0101.21", "0101.29", "0101.30", 
    "0101.90", "0102.21", "0102.29", "0102.31", "0102.39", "0102.90", 
    "0103.10", "CHILE.code"), class = "factor"), V2 = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("CC", "CHILE.psr"
    ), class = "factor"), V3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("", "CHILE.index"), class = "factor")), .Names = c("V1", 
    "V2", "V3"), class = "data.frame", row.names = c(NA, -11L))

The result I want to show is like this.

structure(list(V1 = structure(c(11L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L), .Label = c("0101.21", "0101.29", "0101.30", 
"0101.90", "0102.21", "0102.29", "0102.31", "0102.39", "0102.90", 
"0103.10", "CHILE.code"), class = "factor"), V2 = structure(c(2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("CC", "CHILE.psr"
), class = "factor"), V3 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("6", "CHILE.index"), class = "factor")), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -11L))

Thank you for all and merry christmas.

Aucun commentaire:

Enregistrer un commentaire