在 stars 对象中重新分配单元格值 NA

Reassign cell value NA in stars object

我最近开始使用stars R包。我正在努力将 NA 值重新分配给“未知”。我找到了一个潜在的解决方案 here,但它似乎不适用于 NA。非常感谢解决此问题的任何建议。出于某种原因,SO 不喜欢没有添加更多信息的大量代码,所以我在底部添加了一些随机文本。

这是我使用的代码:

    a <- which(is.na(BenClass[[1]]))
    BenClass[[1]][a] = 1
    
    Warning message:
    In `[<-.factor`(`*tmp*`, a, value = c(NA, NA, NA, NA, NA, NA, NA,  :
      invalid factor level, NA generated
    
    unique(BenClass$habitat)

    #The stars object
        ben <- structure(list(habitat = structure(c(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, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, NA, 4L, 4L, NA, 4L, 4L, 
4L, 4L, 4L, NA, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, NA, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, NA, NA, NA, 4L, 1L, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 4L, NA, 1L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1L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L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 
NA, NA, 4L, 2L, 4L, 5L, 4L, 4L, 4L, NA, 2L, 2L, 4L, 4L, NA, NA, 
NA, NA, NA, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, 2L, 5L, 5L, 4L, 4L, 4L, 3L, NA, 4L, 2L, 4L, 4L, NA, NA, NA, 
NA, NA, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 2L, 
4L, 2L, 2L, 4L, 4L, 4L, NA, NA, 4L, 2L, 4L, 4L, NA, NA, NA, NA, 
NA, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 
NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 4L, 
5L, 5L, 5L, 4L, NA, NA, 1L, 2L, 2L, 4L, 4L, NA, NA, NA, NA, NA, 
4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 2L, 5L, 3L, 
4L, 4L, NA, NA, NA, 2L, 4L, 4L, 4L, 4L, NA, NA, NA, NA, NA, 4L, 
2L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, NA, 
NA, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 5L, 4L, 5L, 4L, 
4L, NA, NA, NA, 5L, 5L, 2L, 1L, NA, NA, NA, NA, NA, NA, NA, 2L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, NA, NA, 
NA, NA, 1L, NA, NA, NA, NA, NA, NA, 2L, 5L, 2L, 5L, 5L, 4L, NA, 
5L, NA, NA, 4L, 5L, 2L, 2L, NA, NA, NA, NA, NA, NA, NA, 3L, 5L, 
5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, NA, 1L, NA, 
NA, 4L, NA, NA, NA, NA, NA, NA, 2L, 5L, 5L, NA, 4L, 4L, NA, 4L, 
1L, 2L, 2L, 5L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 5L, 5L, 
5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 5L, 2L, 5L, 5L, 4L, NA, NA, 4L, 2L, 
2L, 4L, 4L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 1L, 5L, 5L, 
4L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 2L, 5L, 5L, 5L, 4L, NA, 2L, 4L, 4L, 2L, 2L, 
4L, 4L, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 1L, 4L, 5L, 
5L, 5L, 5L, 4L, 4L, 4L, 5L, 5L, 5L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1L, 2L, 1L, 4L, 4L, NA, 2L, 2L, 2L, 1L, 2L, 5L, 
4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 4L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, NA, 1L, NA, NA, NA, NA, NA, NA, 
NA, NA, 1L, 2L, 5L, 4L, 4L, NA, 2L, 3L, 2L, 5L, 4L, 2L, 1L, NA, 
NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, 4L,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NA, NA, NA, NA, NA, NA, 4L, 4L, 4L, 4L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 4L, 2L, 5L, 3L, 3L, 3L, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, NA, NA, NA, 1L, NA, NA, 4L, 4L, 4L, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 5L, 4L, NA, NA, 1L, NA, NA, NA, 4L, 4L, NA, NA, 1L, 4L, 
2L, 2L, 2L, NA, 5L, 2L, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 2L, 3L, 4L, 3L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 
3L, 5L, 4L, NA, NA, 1L, NA, NA, NA, 4L, 4L, NA, 5L, 4L, 5L, 5L, 
1L, 1L, 2L, 1L, 5L, 3L, 4L, 4L, 4L, 5L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 5L, 5L, 5L, 2L, 2L, 3L, 4L, 4L, 4L, 4L, 3L, 5L, 
4L, 4L, NA, NA, 1L, NA, NA, NA, 4L, 4L, 5L, NA, 2L, 2L, 5L, 3L, 
1L, 2L, 5L, 2L, 2L, 5L, 4L, 4L, 5L, 4L, 5L, NA, NA, NA, NA, NA, 
NA, NA, 4L, 3L, 2L, 5L, 5L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, NA, NA, NA, NA, NA, NA, 4L, 4L, 5L, 2L, 2L, 3L, 5L, 3L, 2L, 
5L, 3L, 5L, 5L, 5L, 4L, 5L, 3L, 3L, 1L, 2L, NA, NA, NA, NA, NA, 
4L, 5L, 5L, 2L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L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 5L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
NA, NA, NA, NA, 1L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, NA, 
NA, NA, NA, 1L, 5L, 2L, 5L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 5L, 3L, 
4L, 5L, 4L, 5L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 
4L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, NA, NA, NA, NA, NA), .Dim = c(x = 47L, 
y = 76L), .Label = c("Coral/Algae", "Rubble", "Rock", "Seagrass", 
"Sand"), class = "factor")), dimensions = structure(list(x = structure(list(
    from = 1, to = 47L, offset = 668157.947228626, delta = 250, 
    refsys = structure(list(input = "EPSG:32617", wkt = "PROJCRS[\"WGS 84 / UTM zone 17N\",\n    BASEGEOGCRS[\"WGS 84\",\n        DATUM[\"World Geodetic System 1984\",\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        ID[\"EPSG\",4326]],\n    CONVERSION[\"UTM zone 17N\",\n        METHOD[\"Transverse Mercator\",\n            ID[\"EPSG\",9807]],\n        PARAMETER[\"Latitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8801]],\n        PARAMETER[\"Longitude of natural origin\",-81,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"Scale factor at natural origin\",0.9996,\n            SCALEUNIT[\"unity\",1],\n            ID[\"EPSG\",8805]],\n        PARAMETER[\"False easting\",500000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    USAGE[\n        SCOPE[\"Engineering survey, topographic mapping.\"],\n        AREA[\"Between 84°W and 78°W, northern hemisphere between equator and 84°N, onshore and offshore. Bahamas. Ecuador - north of equator. Canada - Nunavut; Ontario; Quebec. Cayman Islands. Colombia. Costa Rica. Cuba. Jamaica. Nicaragua. Panama. United States (USA).\"],\n        BBOX[0,-84,84,-78]],\n    ID[\"EPSG\",32617]]"), class = "crs"), 
    point = NA, values = NULL), class = "dimension"), y = structure(list(
    from = 1, to = 76L, offset = 2856635.07182904, delta = -250, 
    refsys = structure(list(input = "EPSG:32617", wkt = "PROJCRS[\"WGS 84 / UTM zone 17N\",\n    BASEGEOGCRS[\"WGS 84\",\n        DATUM[\"World Geodetic System 1984\",\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        ID[\"EPSG\",4326]],\n    CONVERSION[\"UTM zone 17N\",\n        METHOD[\"Transverse Mercator\",\n            ID[\"EPSG\",9807]],\n        PARAMETER[\"Latitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8801]],\n        PARAMETER[\"Longitude of natural origin\",-81,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"Scale factor at natural origin\",0.9996,\n            SCALEUNIT[\"unity\",1],\n            ID[\"EPSG\",8805]],\n        PARAMETER[\"False easting\",500000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    USAGE[\n        SCOPE[\"Engineering survey, topographic mapping.\"],\n        AREA[\"Between 84°W and 78°W, northern hemisphere between equator and 84°N, onshore and offshore. Bahamas. Ecuador - north of equator. Canada - Nunavut; Ontario; Quebec. Cayman Islands. Colombia. Costa Rica. Cuba. Jamaica. Nicaragua. Panama. United States (USA).\"],\n        BBOX[0,-84,84,-78]],\n    ID[\"EPSG\",32617]]"), class = "crs"), 
    point = NA, values = NULL), class = "dimension")), raster = structure(list(
    affine = c(0, 0), dimensions = c("x", "y"), curvilinear = FALSE), class = "stars_raster"), class = "dimensions"), class = "stars")

好吧其实很简单。有点麻烦,但它是这样工作的:

BenClass$habitat <- as.character(BenClass$habitat)
a <- which(is.na(BenClass[[1]]))
BenClass[[1]][a] = "Unknown"