# Re: [R-sig-Geo] Problem with writing a function that attaches atributes from polygons of a shapefile to the points of a grid

```Hi Miguel,
Maybe this way:
```
```
xy.df <- as.data.frame(xy)
xy_sf <- st_as_sf(xy.df, coords=1:2)
xy_crs <- st_sfc(xy_sf, crs=25830)
xy_joined <- st_join(xy_crs, Catfiscal)
```
join.ppp <- ppp(st_coordinates(xy_joined)[,"X"], st_coordinates(xy_joined)[,"Y"], window=madrid_sqr_m, marks =xy_joined\$categoria)
```
Cheers,
Marcelino

El 23/01/2023 a las 20:11, MIGUEL GOMEZ DE ANTONIO escribió:
```
```[No suele recibir correo electr?nico de m...@ccee.ucm.es. Descubra por qu? esto

Hi all,

I need to create a function that georreference covariates that are not
available at the point level but at a census tract or any other
administrative polygon level (cities, regions, etc.).

My last goal is to create my own function that attaches the values of a
census tract shapefile to the points of the grid in an observation window.
Then I would like to kernel smooth it to obtain a covariate that is
continouos in the observation window that contains the grid points.

I am guessing that my problem is either in creating an object of the class
sf when adding the dataframe to the sfg object or when using the function
st_join () that is supposed to add the attributes of the polygon to the
points located in it.

#I first created the coordinates of the points of the grid in the
"madrid_sqr_m" is just the boundary of the grid (class owin)

#Then I created a sfg object multipolygon

x=as.numeric(xy\$x)

y=as.numeric(xy\$y)

XY=data.frame(x,y)

xy=as.matrix(XY)

xy=st_multipoint(xy)#creo el objeto sfg que es un conjunto de puntos
(multypoligon)

xy_crs=st_sfc(xy, crs=25830)#add the proyected reference system

xy_crs

#add the dataframe to have an object of class sf. NOT SURE IF I AM DOING
THIS RIGHT. THE GRID POINTS HAVE NOT ATTRIBUTES SO THE DATAFRAME SHOULD BE
EMPTY, ISN`T?

df <- data.frame(nombre = c("grid"))#

grid_sf=st_sf(df, xy_crs)#

class(grid_sf)# #Add the covariates of a shapefile named "Catfiscal", which
is a census tract polygon, to the points of the grid

join<-st_join(grid_sf,Catfiscal)# PROBLEM MIGHT BE HERE BECAUSE IS NOT
ASIGNING TO EACH GRID POINT THE COVARIATES OF THE POLIGONS

x=join\$categoria# "categoria" is the covariate I would like to add to the
points of the grid once I convert the sf object into a "ppp" object of the
Spatstat

the boundary of the grid (class owin)

grid_con_cat_f.ppp<-setmarks(join.ppp,x) **

*#Error in `marks<-.ppp`(`*tmp*`, value = value) : # number of points !=
number of marks ** *

density.CATF<-Smooth.ppp(grid_con_cat_f.ppp)

I have been stuck on this several weeks os any advice with be very much
appreciated

Miguel

[[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
```
```

--
Marcelino de la Cruz Rot