1

I am new in R. When using a code I got I get the error "konnte Funktion "write.vector" nicht finden" (could not find function "write.vector"). The code looks like this: write.vector(x, ""). The person who wrote the code (in the last summer) said, she didn't needed to install any type of package for that.

I googled for the problem and found this: Error: could not find function ... in R? I checked the things mentioned here but it doesn't helped. The spelling is correct, because I just copy pasted the code, which worked well for the other person. Also I don't know which package I could install that might be missing for that. I didn't find any with help.search("some.function") or find. When using the RSiteSearch("some.function") I found this: http://finzi.psych.upenn.edu/R/library/quantable/html/write.vector.html, which also didn't help me with my problem.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
P.idger
  • 11
  • 3
  • 1
    Download `quantable` package, load it and use that function! – Duck Jan 14 '21 at 13:14
  • also any reason you're using write.vector? based on the documentation, it's writing a table anyway. You could use write.csv or saveRDS instead – latlio Jan 14 '21 at 13:15

2 Answers2

0

As stated by @Duck in the comments, the reason it can't be found, is that the package containing the function is not installed or loaded. A quick google search tells me it quantable, so we could check if that is installed and load it using something like the code below:

#test if installed, if not install it.
if(!require(quantable, quiet = TRUE))
  install.packages("quantable")
library(quantable)
Oliver
  • 8,169
  • 3
  • 15
  • 37
  • Thank you for your answers! I installed quantable package, but now i see this error: Error in file(file, ifelse(append, "a", "w")) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, "a", "w")) : cannot open file 'd:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs/Vector_Cologne': Permission denied This also happens when i usewrite.csv. Why could this be. I dont understand why i get the error cannot open file because with the "write.vector" i don't even want to open a file, but safe it, or am i wrong with this? – P.idger Jan 31 '21 at 15:15
  • P.S.: I read this Thread:https://stackoverflow.com/questions/17156445/why-i-get-this-error-writing-data-to-a-file and opened R Studio as Administrator, but i still see the error – P.idger Jan 31 '21 at 15:55
  • My pleasure. When experiencing file access problems it is often concerned with permission rights, as you tried using administrator rights might work out. Can you open the file outside of R or is that also a problem? You could try creating a copy (ctrl + c -> ctrl + v) and see if the problem persists on the copied file. – Oliver Jan 31 '21 at 19:25
  • I have no "file access problems" because there is no file until now. I thought the write.vector function is used to write the Vector file i created in R in a folder. This folder i created for that purpose and it its empty until now. So i thought its more a "folder access problem" – P.idger Feb 01 '21 at 10:47
  • I "answered" the code i used above if that may help – P.idger Feb 01 '21 at 11:11
  • Ah Hi @P.idger, I apologize, I had forgotten the context of the question due to the time difference. `write.vector` wraps around `write.table` and should create the file by itself. Now I am assuming your file path is corrupt (wrongly spelled) or does not name an actual file. For example: Is `"d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs/Vector_Cologne"` a valid file path or is it a folder? – Oliver Feb 01 '21 at 12:58
  • No Problem, it took a while because i had to deal with some previous data Problems first, so i forgot about this one here ;) "d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs/Vector_Cologne" is just a folder because looking at the code i thought i have to create one, where the Vector then can be written into. So what should i do here instead? My Co-Study who gave me the code said, as far as she remembers, she also created an empty folder here and put it in the code at that place – P.idger Feb 01 '21 at 19:08
  • A pure guess, is that you're co-worker has an empty folder, and then specify the file in the second argument to be within the folder. Basically the folder expects the name of a file to use, and not a folder itself (it does not automatically pick a file name). So an option would be adding a file name (`"---/vector.csv"`) to your path (choose a file name that makes sense in your problem of course!). It is similar to `writeRaster`, `write.csv` and so on. – Oliver Feb 01 '21 at 20:28
  • If you have to run this continuesly a valid option is to use the `tempfile` function to generate names. For example `tempfile(format(Sys.time(), '%y-%m-%d-%H:%M:%S-%Z'), tmpdir = "d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs/Vector_Cologne")` is a valid option. This generates a file (name), with the current date/time/timezone and a random character string following the file. Often just using Date-H-M is sufficient (without `tempfile`), unless you have to run this continuesly, or you could choose a single name, if this is only "occasionally" used. – Oliver Feb 01 '21 at 20:32
  • In short: Add a file name to your existing string. :-) – Oliver Feb 01 '21 at 20:33
  • 1
    Hey Oliver, that worked out, thanks a lot! – P.idger Feb 02 '21 at 18:58
0

This is the code I used until the error if that may help:

Stanzen von Rasterstack mit Stadtgrenze Köln
#libraries

    library(raster)

    library(rgdal)
    library(nortest)
    install.packages("sp", dependencies=TRUE)

    #test if installed, if not install it.
    library(sp)
    if(!require(quantable, quiet = TRUE))
      install.packages("quntable")
    library(quantable)
    setwd("d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs")
    ## polygon with crop-extend ##
    files <- list.files("d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs", include.dirs = F, full.names =
                          T)
    shape_data <- readOGR("d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/Shapefiles/VerwaltungsgrenzeKoeln/VerwaltungsgrenzeKoeln.shp",
                          stringsAsFactors=FALSE)
    plot(shape_data)
    ## load tif files ##
    infiles = list.files(path=getwd(),
                         pattern="*.tif$|*.TIF$")
    ## Filenames with desired suffix and output place ##
    outfiles = file.path("d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/Cologne_Rasterstack",
                         paste0(basename(tools::file_path_sans_ext(infiles)),
                                ".tif"))
    outfiles[outfiles == -9999] <- NA #alle -9999 auf NA setzen, wenn nötig
    ## crop and output settings (compression and datatype)
    for (i in seq_along(infiles)) {
      r = crop(stack(infiles[i]), shape_data)
      writeRaster(r, filename=outfiles[i],
                  bylayer=FALSE,
                  format="GTiff",
                  options="COMPRESS=ZIP",
                  x, NAflag=-9999,
                  overwrite=TRUE)
    }
    dataType(r)
    View(r)
    # load files
    files <- list.files("d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/Cologne_Rasterstack", include.dirs = F, full.names = T)
    #stack raster
    rasterstack <- stack(lapply(files, raster))
    r <- rasterstack
    r[] <- 1:length(r)
    ## crop and mask
    r2 <- crop(r, extent(shape_data))
    r3 <- mask(r2, shape_data)
    plot(r2)
    rc <- crop(rasterstack, extent(r3))
    rcm <- mask(rc, shape_data)
    as.vector(rcm)
    x <- as.vector(rcm)
    plot(rcm)
    write.vector(x, "d:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs/Vector_Cologne")
P.idger
  • 11
  • 3