【问题标题】:R could not find function "write.vector" - what can I do?R 找不到函数“write.vector” - 我该怎么办?
【发布时间】:2021-01-14 13:13:22
【问题描述】:

我是 R 的新手。使用我得到的代码时,我收到错误“konnte Funktion”write.vector“nicht finden”(找不到函数“write.vector”)。代码如下所示:write.vector(x, "")。写代码的人(去年夏天)说,她不需要为此安装任何类型的包。

我用谷歌搜索了这个问题,发现:Error: could not find function ... in R?我检查了这里提到的东西,但没有帮助。拼写是正确的,因为我只是复制粘贴了代码,这对其他人来说效果很好。另外我不知道我可以安装哪个可能会丢失的软件包。我没有找到任何 help.search("some.function") 或找到。使用RSiteSearch("some.function") 时,我发现了这个:http://finzi.psych.upenn.edu/R/library/quantable/html/write.vector.html,这对我的问题也没有帮助。

【问题讨论】:

  • 下载quantable包,加载并使用该功能!
  • 还有什么理由使用 write.vector?根据文档,无论如何它都在写一个表格。您可以改用 write.csv 或 saveRDS

标签: r function


【解决方案1】:

这是我在出现错误之前使用的代码,如果有帮助的话:

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")

【讨论】:

    【解决方案2】:

    正如@Duck 在 cmets 中所说,找不到它的原因是未安装或加载包含该功能的包。一个快速的谷歌搜索告诉我它quantable,所以我们可以检查它是否已安装并使用如下代码加载它:

    #test if installed, if not install it.
    if(!require(quantable, quiet = TRUE))
      install.packages("quantable")
    library(quantable)
    

    【讨论】:

    • 感谢您的回答!我安装了可量化的软件包,但现在我看到了这个错误:文件中的错误(文件,ifelse(追加,“a”,“w”)):无法打开连接另外:警告消息:在文件中(文件,ifelse(追加,“a”,“w”)):无法打开文件'd:/Users/paulb/Desktop/Uni/Bachelorarbeit/R/GeoTiffs/Vector_Cologne':权限被拒绝当我使用write.csv时也会发生这种情况。为什么会这样。我不明白为什么我得到错误无法打开文件,因为使用“write.vector”我什至不想打开文件,但它是安全的,还是我错了?
    • P.S.:我读了这个 Thread:stackoverflow.com/questions/17156445/… 并以管理员身份打开了 R Studio,但我仍然看到错误
    • 我的荣幸。当遇到文件访问问题时,它通常与权限有关,因为您尝试使用管理员权限可能会奏效。您可以在 R 之外打开文件还是这也是一个问题?您可以尝试创建一个副本(ctrl + c -> ctrl + v),看看复制的文件是否仍然存在问题。
    • 我没有“文件访问问题”,因为到目前为止还没有文件。我认为 write.vector 函数用于将我在 R 中创建的 Vector 文件写入文件夹中。我为此目的创建了这个文件夹,直到现在它都是空的。所以我认为它更像是一个“文件夹访问问题”
    • 嘿奥利弗,成功了,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2013-12-17
    • 1970-01-01
    相关资源
    最近更新 更多