0

I have a data frame with x, y, values and unique IDs of 50 locations. I converted this data frame to a raster, but I lost the unique IDs of these locations. How can I preserve the name of each location within the created raster?

Pred13 = data.frame(x,y,Index)
coordinates(Pred13) = ~x+y
rst_pred_13 <- raster()
extent(rst_pred_13) <- extent(Pred13)
Pred_Yld13 = rasterize(Pred13, rst_pred_13, Pred13, fun = mean) 

This is just convert to raster, but the names (unique ID) are not there anymore.

ID      x           y           Index
D103    574070.6    4452182.5   0.716371109
D104    574969.4    4452182.5   0.744886447
D105    575868.2    4452182.5   0.748975528
D106    576767      4452182.5   0.766794456
S107    577665.8    4452182.5   0.693888885
S108    578564.6    4452182.5   0.711575956
S109    579463.4    4452182.5   0.665713451
S110    580362.2    4452182.5   0.66711642
S111    581261      4452182.5   0.744038446
F112    582159.8    4452182.5   0.698995257
F113    583058.6    4452182.5   0.645033871
F114    583957.4    4452182.5   0.661884746
F115    584856.2    4452182.5   0.661850527
F116    585755      4452182.5   0.59968895
R117    586653.8    4452182.5   0.693592996
R118    587552.6    4452182.5   0.744853835
M119    588451.4    4452182.5   0.746308897
M120    589350.2    4452182.5   0.742065499
M121    590249      4452182.5   0.715676129
M122    591147.8    4452182.5   0.662452103
camille
  • 16,432
  • 18
  • 38
  • 60
  • Instead of posting the same question twice, you should look at the guidance on what to do with the first question having been closed https://meta.stackexchange.com/help/reopen-questions – camille Dec 30 '19 at 03:12
  • I edited the question and I don't know what is missing in it! I think it's quite clear now. – user236137 Dec 30 '19 at 06:25
  • It looks like you deleted the first question, right? That's fine, we just don't want to have multiple posts of the same question. It will be easier to help, though, if you make this a little more [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). A `dput` would be helpful, and I'm still unsure (like I mentioned on the other post) where in the code this data would come in; where do the IDs come from? – camille Dec 30 '19 at 15:01

1 Answers1

0

I solved this and this is the way I did it. Thanks anyway.

df = data.frame(test, na.rm=T)
x=df$x
y=df$y
Index=df$Index
Id_un = unique(df$ID)
for(i in Id_un){
  Pred13 = data.frame(x,y,Index)
  rst = rasterFromXYZ(Pred13)
  names(rst) = unique(i)
  rst$newID = as.factor(i)
}