How to join a DF object and a SF object ?

Hi ! I am new on R studio and I cannot put my variable (TXPAUVR which represents the percent of poverty in NIcaragua) on my map.
I can visualize the map :

    mf_map(x=NIC, col = "white", border = "black")

But then I cannot add my variable :

NEW <- NEW %>%
theme(panel.border=element_blank(), axis.line = element_line())

mf_map(x = NEW,
var = "TXPAUVR",
type = "choro",
pal = viridis(3),
breaks = BK1,
leg_title = "Taux de pauvreté",
leg_pos = "topleft",
add = TRUE)

The error either is : Error in if (xtype == "LINE") { : missing value where TRUE/FALSE needed

Or : Error in x[[var]] : attempt to select less than one element in get1index

Or : Error in st_sfc(crs = crs) :
is.numeric(crs) || is.character(crs) || inherits(crs, "crs") is not TRUE

Apparently there is a problem with the class of my Data frame (NEW) in which I put the TXPAUVR variable. It refuses to be considered as "numeric".

Could you help me to solve on eof these errors ? I would be very grateful...

Hi @ LINALGBS, welcome to the forum.
Have a look at FAQ: How to do a minimal reproducible example ( reprex ) for beginners

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need.

Another, simple approach is to just paste code and the results of dput() between two


unfortunately it is very difficult to understand where the problem is.
you can you maybe try this,

region_sf <- sf::st_transform(x = NIC, crs = 4326)

But indeed we need to have a reprex in order to understand how to help you.

Have a look at FAQ: How to do a minimal reproducible example ( reprex ) for beginners


Dear @tacoba ,

Thanks for your help. I will look at the FAQ you both sent me.

It is hard to me to explain where the problem is because it changes every time I had or remove something. But I have few questions in mind that could help me to understand how to handle the package map_sf

I learnt that there are 3 steps to create a map (cartography)

  • to create the map background
  • to add the variables
  • to modify the layout

I succeed the 1st one (map of Nicaragua with municipality boundaries).

The issue is when I try to add my variables which are the poverty rate and the drop-out rate. My question is : do I have to join my 2 data Frames ("NIC" that is a .shp used to plot the map + "MD" that is a .xls that contains the poverty rate and drop-out rate per municipalities) and work on a unique object ?

I did this :

DF <- left_join(MD, NIC, by ="Municipios")

I could not use "DF" with mf_map because the class of the object was not good (indeed it had a column dedicated to "geometry" since I left-joined the Data Frame "NIC" ). I thus changed its type of class.

attributes(DF)$class <- "sf"

But it did not solve my problem.

  1. When I tried to visualize the poverty rate "TXPAUVR", the error message was also about its class because TXPAUVR was not numeric (which is strange since it seems to be numeric when I look on the data frame) + I could not force TXPAUVR to become a numeric variable

  2. When I created a new Data Frame "NEW" in which I only put the name of the municipalities, TXPAUVR and TXSCO (drop-out rate), the error changed and became this one :

Error in if (xtype == "LINE") { : missing value where TRUE/FALSE needed

  1. There was also this error message that I do not understand

Error in x[[var]] : attempt to select less than one element in get1index

I hope it is a bit more clear... I am sorry if I did not use the wright vocabulary :slight_smile:


i know sometimes you see numbers and think it is numeric but it is not...

in order to see if it is really numeric you could type this:

is.numeric ("TXPAUVR")

if it is false then you will need to change it to numeric.

Maybe you can try first this and let me know what you got.


Hello !! I think I found what the core of my problem was. I have a problem with the package "map_sf" because I tried to use it on an object that is no longer a "sf"

Let me explain. I have a shp file (map of Nicaragua with its boundaries) and a xlsx file (data about Nicaragua such as poverty rate per municipality).

I want to join them in order to create a map with "map_sf". To do so, I know that my object needs to be both "Data Frame" and "sf". It is the case for my first object (NIC.sf) but when I join the second object (MD.df) it does not conserve the "sf" class whereas I still have the geometry column in my new object (SHP)

Here is my code :slight_smile:

NIC.sf <- st_read(dsn = "F:/Master/R Studio/DATA/ADM/NIC_adm2.shp",
crs = 5462,
stringsAsFactors = F)

[1] "sf" "data.frame"

[1] "data.frame"

#then I create a new object

SHP <- left_join(MD.df, NIC.sf, by ="Municipios")
[1] "data.frame"

Could you help me to join MD.df and NIC.sf to create SHP as a new object that is both "Dataframe" and "sf" ?

Thanks a lot !!!


It is very difficult for me to be able to help you without a reprex but i believe this link might help you.

All the best,

the vignettes for sf are good sources to learn from.
They are clear about this
4. Manipulating Simple Features (
The usual join verbs of base R (merge ) and of dplyr (left_join , etc) work for sf objects as well; the joining takes place on attributes (ignoring geometries). In case of no matching geometry, an empty geometry is substituted. The second argument should be a data.frame (or similar), not an sf object (emphasis mine):

in your code you have a dataframe as x and a shapefile as y, which will not work via the sf methods.
for different effects, in terms of join direction; use right_join, if you would have used left_join() but for object types at the x,y positions, since these are mirrored functions.

This topic was automatically closed 21 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.