0

我有以下脚本:

Files <- list.files(datapath, pattern = ".txt")


# convert all .txt files in the given folder
for (i in 1:length(Files)){
  dataname <- Files[i]
  filename <- paste(datapath, dataname, sep="")

  read_eprime(filename) %>% FrameList() %>% to_data_frame() %>% select(Cue:ISIslide.RT, GetReady.OnsetTime, ScanWait.OffsetTime) %>% slice (-1) %>%  
    fill(GetReady.OnsetTime, .direction = "up") %>% fill(ScanWait.OffsetTime, .direction = "up") %>% slice (-79) -> edf


  write.csv(edf, file = paste0(datapath, dataname, "_P.csv"), na = "", row.names = FALSE)

  #write.csv(edf, file=paste0(datapath, "tsv_", dataname), quote=FALSE, sep="\t", na = "")
}

如何调整它,使输出文件不保存名称中间的“.txt”?

4

2 回答 2

2

检查file_path_sans_ext获取文件名。这来自内置工具包,将仅返回不带扩展名的文件名。

tools::file_path_sans_ext("myFile.txt")
## [1] "myFile"

您也可以使用它而不是将文件路径粘贴到文件名。

tools::file_path_sans_ext("Downloads/Stuff/myFile.txt")
## [1] "Downloads/Stuff/myFile"
于 2018-07-06T20:39:46.257 回答
0

您可以考虑用from datanamegsub替换为:.txt""

gsub("\\.txt","","file.txt")
#[1] "file"

dataname <- "myFile.txt"

gsub("\\.txt","",dataname)
#[1] "myFile"

dataname <- "Downloads/Stuff/myFile.txt"
gsub("\\.txt","",dataname)
#[1] "Downloads/Stuff/myFile"

好的部分gsub是它也可以更正多次出现的“.txt”。例如:

dataname <- "Downloads/Stuff/my.txtFile.txt"
gsub("\\.txt","",dataname)
#[1] "Downloads/Stuff/myFile"
于 2018-07-06T21:19:31.440 回答