as.asc error cannot find function

Hi there!

I was looking at making a conversion from a csv file to an asc. extension. I found the following code, but for some reason it gives me an error as it cannot find the function as.asc nor export.asc.

I downloaded the following packages:

library(sdm)
library(sp)
library(deldir)
library(raster)
library(ade4)
library(adehabitatMA)
library(adehabitatHR)
library(adehabitatLT)

And this is my original code:

distance <- read.csv("../Final tables/Distance_Spring_2000_2010.csv", header=TRUE)
library(sdm)
library(sp)
library(deldir)
library(raster)
library(ade4)
library(adehabitatMA)
library(adehabitatHR)
library(adehabitatLT)
csv <- data.frame(x = distance$lon, y = distance$lat, cadmium = distance$distance)
csv <- as.matrix(csv)

csv <- as.asc(csv)
export.asc(csv, "ASCFILE.asc")

As well as the error I get:

export.asc(csv, "ASCFILE.asc")
Error in export.asc(csv, "ASCFILE.asc") :
could not find function "export.asc"
csv <- as.asc(csv)
Error in as.asc(csv) : could not find function "as.asc"

I am a novice with R so I don't really understand what is going on... It is because it requires a package that is not available anymore?

Thank you for any help !

Aylis

R is such a vast landscape of complexity and that makes it easy to think there has no simplicity.

To check on the immediate feedback from the error

`?? export.asc

which confirms that it's missing and code using it will fail.

But that's only the most immediate problem. Usually, the hardest problem in troubleshooting R is coming up with the right question. Let's ask, instead

What is the difference between csv and asc and why do we care?

The easy answer is that we care if some resource we need demands one form rather than the other.

The difference lies in the purpose. A csv format is a layout convention (comma separated values) for data. The data can, in theory, be anything, but in practice is limited to numbers and characters. Discrete pieces of the data are generally separated by commas (corresponding to column variables) and newlines (corresponding to rows). It's a universal format because every system likely to open a csv file can do so with just the resources available from the operating system.

An asc file has no format expectations. It reflects a coding convention. It will happily read csv files but does not require delimiters. It does, however, impose an encoding restriction—all bytes must be encoded within a 128 character convention. The allowable characters include the 96 numbers, symbols and unaccented lower- and upper-case letters appearing on keyboards. No diacritical marks, no Greek, Cyrillic or other characters allowed; if included they will show up with a single symbol signifying "huh?"

The code reads Distance_Spring_2000_2010.csv and assigns it to the object distance. From distance it constructs data frame named csv that contains three variables from the distance file and then proceeds to convert it to a matrix object. Finally, failed attempts are made to use csv as an argument to the missing export.asc and as.csv functions.

Every R problem can be thought of with advantage as the interaction of three objects— an existing object, x , a desired object,y , and a function, f, that will return a value of y given x as an argument. In other words, school algebra— f(x) = y. Any of the objects can be composites.

Here, x is the csv object. In default of any other information, it must be assumed that y is identical to x but for its character encoding. If that is the case, the part of f can be played by iconv

x <- c("Ekstr\xf8m", "J\xf6reskog", "bi\xdfchen Z\xfcrcher")
Encoding(x) <- "latin1"
x
#> [1] "Ekstrøm"         "Jöreskog"        "bißchen Zürcher"
try(iconv(x, "latin1", "ASCII//TRANSLIT"))  # platform-dependent
#> [1] "Ekstrom"          "Joreskog"         "bisschen Zurcher"

I've been didactic, however. It looks that what you really want is the long/lat coordinates and the distance (to be renamed Cadmium back on disk as a csv file that can be read in later.

If that's the case

csv <- data.frame(x = distance$lon, y = distance$lat, cadmium = distance$distance)
write.csv(csv, file = "mycsv.csv")

If you want to deal with encoding

write.csv(csv, file = "mycsv.csv", encoding = "ASCII")

but if these data are all numeric, that should not be a concern.

Consider using {readr} for better csv facilities and {sf} for easier integration of GIS with other data.

Hi there,

Thank you so so much for your quick answer.
I feel that I tend to disregard basic concepts but I am starting to understand the process.

I did try out the following code, however I am still getting an error :

Error in write.table(csv, file = "mycsv.csv", encoding = "ASCII", col.names = NA, :
unused argument (encoding = "ASCII")

It does feel like the problem lies in the encoding part, or I am asking the wrong question?

Thank you so much for your help, it means a lot.

Aylis

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.