Package sf ::how to avoid unwanted column shifts?

Hi, I made a function myfunction which used left_join from dplyr.

I obtain the error message :

Error in left_join():
! Can't join x$i with y$EPT_NUM due to incompatible types.
:information_source: x$i is a .
:information_source: y$EPT_NUM is a .

This messages is due to

dff.jointure <- DF.flow %>%
                                    left_join(df.point, by = setNames(id, nm = origin)) %>%
                                    rename(Xi = x, Yi = y) %>%
                                    left_join(df.point, by = setNames(id, nm = destination)) %>%
                                    rename(Xj = x, Yj = y)

I think I know where is a problem but I don't know to fix.
I received the error message because the data in my function is created as below

myfunction(map,code
  carte_sf <- st_as_sf(map, "sf")
  contig<-st_intersects(x = carte_sf, y = carte_sf, sparse=FALSE, prepared = TRUE)
  colnames(contig)<-carte_sf$code
  rownames(contig)<-carte_sf$code
  
  for (i in 1:nrow(contig)) {
    for (j in 1:ncol(contig))
    {
      if (contig[i, j] == TRUE) {
        contig[i, j] <- 1
      }
      if (contig[i, i] != 0) {
        contig[i, i] <- 0
      }
    }
  }
  tab <- flowtabmat(contig, matlist = "L")
  colnames(tab) <- c("i", "j", "ordre")
  ordre_1 <- tab[tab[, "cij"] != 0, ]

The ouput myfunction
Capture d’écran du 2023-10-10 07-31-17

I've tested the code inside of my function and I obtain
Capture d’écran du 2023-10-10 07-37-45

As you can see the type is different between two output.
Plus when write in CSV ordre_1 there is a colonn added I think it's here the problem.

Capture d’écran du 2023-10-10 08-07-56

This shifft will explain the error message!

I hope it's clear; Have you any suggestions how I can fix this ?

I've a print in print(carte_sf$code) and the result is NULL

How can I write rownames(contig)<-carte_sf$code so that it passes in my function?
I will appraciate your support!

I think that come from st_intesercts()

map <- st_read("data MGP/MGP_TER.shp")
myfunction(bkg,code){
carte_sf <- st_as_sf(bkg, "sf")
 contig<-st_intersects(x = carte_sf, y = carte_sf, sparse = FALSE)
}

the output is :

[1] "matrix" "array" 
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
 [1,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 [2,]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
 [3,]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [4,]  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [5,]  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
 [6,]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
 [7,] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
 [8,]  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
 [9,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
[10,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
[11,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
[12,]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE

I would like to modify rows and column name from map that is an sf object $EPT_NUM from shape file.
Capture d’écran du 2023-10-10 20-09-54

I write in my function :

  colnames(contig)<-(c(carte_sf$code))
  rownames(contig)<-(c(carte_sf$code))

in this case the letter T is not keep. only a numeric value stay.

Have you an idea how can I fix it?

The solution of my problem is :

var<-paste(carte_sf[[code]],sep="")
 colnames(contig)<-var
rownames(contig)<-var

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.