0

我从我的学士论文导师那里得到了这段代码,用 R 在我的计算机上对文件大小进行了四舍五入。在我的计算机 (IOS) 上使用他的代码 (Windows) 似乎是一个问题。

我已经更改了文件夹的路径。setwd 不起作用,我猜这就是文件夹文件名为空的原因。

有人能帮我吗?

谢谢你。下面我复制了 R 代码

library(data.table) 

#Auflösung der Daten anpassen

# 1. Data is located in individual folders, which must be addressed one after the other
path_to_folders <- c("/Users/paul/Desktop/Testdaten")
folder_names <- list.files(path_to_folders)

for(i in 1:length(folder_names)){
  tryCatch({
    # Access relevant data set
    path_to_data <- paste(path_to_folders, "\\", folder_names[i], sep="")           
    setwd(path_to_data)
    filenames <- list.files(path_to_data, pattern =".txt")
    
    for(k in 1:length(filenames)){
    spc_txt_file<-filenames[k]
    spc_txt_dat<-read.table(spc_txt_file)
    spc_txt_dat<-spc_txt_dat[,c(1:3)] # only xyz
    spc_txt_dat <- round(spc_txt_dat, 2)  # Specifying the decimal place
    spc_txt_dat <- unique(spc_txt_dat)    # Double selection
    
    spc_txt_name<-substr(spc_txt_file, 1,5)

    utils::write.table(spc_txt_dat, file = paste0(path_to_data,'/',spc_txt_name, '_gerundet.txt'),
                       row.names = F, col.names = F)}
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
4

1 回答 1

0

我认为这里的问题在于:

path_to_data <- paste(path_to_folders, "\\", folder_names[i], sep="")

IOS 不像 Windows 那样在路径中使用反斜杠。

尝试将其更改为

path_to_data <- paste(path_to_folders, "/", folder_names[i], sep="")
于 2021-11-29T18:53:26.280 回答