【问题标题】:How can I round CSV and Txt filesize on IOS with R如何使用 R 在 IOS 上舍入 CSV 和 Txt 文件大小
【发布时间】:2022-01-06 14:30:24
【问题描述】:

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

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

有人可以帮我吗?

谢谢。下面我复制了 R-Code

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

【问题讨论】:

    标签: ios r windows filesize setwd


    【解决方案1】:

    我认为这里的问题在于:

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

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

    试试改成

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

    【讨论】:

    • 使用 file.path。它将插入任何需要的分隔符。
    • @G.Grothendieck 谢谢!!!
    猜你喜欢
    • 2015-10-07
    • 2015-04-14
    • 1970-01-01
    • 2019-01-14
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多