Coordinates Read as Chategorical Data

Hi everyone,
I have a problem with my coordinates data. I want to make a kernel analysis, but my coordinates data are read as characters. I've tried to use several packages but it always leads to error. Any suggestion for this? Here is my script.
kernel1 <- kerneldata[, c("id", "lon", "lat")]

#make kernel1 data with only id, lon, and lat
View(kernel1)
kernel2 <- na.omit(kernel1)
View(kernel2)
kernel1 <- kerneldata[, c("lon", "lat")]
View(kernel1)
kernel2 <- na.omit(kernel1)
View(kernel2)
kernel3 <- as.numeric(kernel2)
Error: 'list' object cannot be coerced to type 'double'
kernel3 <- as.numeric(unlist(kernel2))
Warning message:
NAs introduced by coercion
summary(kernel2)
lon lat
Length:10543 Length:10543
Class :character Class :character
Mode :character Mode :character
clean <- raw %>%

  • st_as_sf(wkt = "geometry",
  •        crs = 4326)
    

Error in UseMethod("st_as_sf") :
no applicable method for 'st_as_sf' applied to an object of class "function"

clean <- raw %>%

  • st_as_sf(wkt = "geometry",
  •        crs = 4326)
    

Error in UseMethod("st_as_sf") :
no applicable method for 'st_as_sf' applied to an object of class "function"

class(data$lon)
Error in data$lon : object of type 'closure' is not subsettable
class(data$"lon")
Error in data$lon : object of type 'closure' is not subsettable
View(kernel2)
class(data$lat)
Error in data$lat : object of type 'closure' is not subsettable

Hi @RamaAprilio ,

could you post a small subset of the data - it would be easier to help you that way and you will probably get a proper response of why this behaviour occurs.

Hello, thanks for the reply!
This is my short data for the example.

setwd('E:/')
trial1 <- read.csv('book1.csv', header = T,sep = ";")
trial1
id lon lat
1 fe_pre 107,760747 -7,278500972
2 lp_pre 107,77 -7,28
3 hs_pre 107,76 -7,28
4 lu_post_l 107,77 -7,28
5 lu_post_l 107,77 -7,28
6 lu_post_l 107,77 -7,28
7 lu_post_l 107,77 -7,28
8 lu_post_l 107,77 -7,28
9 lu_post_l 107,77 -7,28
10 lu_post_l 107,77 -7,28
11 lu_post_l 107,77 -7,28
12 lu_post_l 107,77 -7,28
13 lu_post_l 107,77 -7,28
14 lu_post_l 107,77 -7,28
summary(trial1)
id lon lat
Length:14 Length:14 Length:14
Class :character Class :character Class :character
Mode :character Mode :character Mode :character

As you can see, my coordinates become characters data. When I tried to change it into numeric, the data becomes NA.

trial1$lon <- as.numeric(trial1$lon)
Warning message:
NAs introduced by coercion

trial2
id lon lat
1 fe_pre NA -7,278500972
2 lp_pre NA -7,28
3 hs_pre NA -7,28
4 lu_post_l NA -7,28
5 lu_post_l NA -7,28
6 lu_post_l NA -7,28
7 lu_post_l NA -7,28
8 lu_post_l NA -7,28
9 lu_post_l NA -7,28
10 lu_post_l NA -7,28
11 lu_post_l NA -7,28
12 lu_post_l NA -7,28
13 lu_post_l NA -7,28
14 lu_post_l NA -7,28

Thank you

Could I ask you to post the data using dput()?

the dput() function gives use an exact copy of the dataset so we can see how your data in organized, it a variable is numeric or a factor, etc. It makes diagnosing a problem much easier. Heru `kerneldata' seems to be iun a list or lists format.

It your case I think we want dput(kerneldata) where "mydata" is the name of your dataset. If it is large datasets probably dput(head(kerneldata, 100)) will do. Paste the output between
```

```

Also, it is a good idea to post code between

```

```

as it gives us better formatted code.

You are using the function st_as_sf(). What package is that from?

Thanks, and welcome to the forum.

1 Like

@RamaAprilio Thanks for sharing just use

read.csv2('book1.csv', header = T)

and everything will work as you expect.

The problem with

trial1 <- read.csv('book1.csv', header = T,sep = ";")

is that it will make it work for your ";" separated file (you are probably working in germany or austria :smiley: so they don't use "," for separation but ; and "," instead of "."for decimal separation). The read.csv function assumes that decimal places are sperated by "." yours are separated by ",". Also R seeing "," will convert to character instead of numeric since it thinks that "," can't be properly converted to numerics.

Or alternatively use

trial1 <- read.csv('book1.csv', header = T,sep = ";", "dec =",")

The read.csv2 function was specifically introduce to work with "weirdly" encoded csv files.

Thank you so much! It works!
I'm still new in R (even in this community) and this answer helps a lot!
Yes, mine was reading "," as decimal separations.
Have a good day and may God blessed you :fairy: !