【问题标题】:Import-loop in R (read.csv)R中的导入循环(read.csv)
【发布时间】:2023-03-24 20:36:01
【问题描述】:

我尝试导入名称中间带有持续数字的 *.csv 文件 - 所以我尝试使用 for 循环生成数字并将其粘贴到文件名中:

for (Number in 20:30){
  (paste("Test.",Number,"File"))<- read.csv("~/Test.-",Number,"File".csv", sep=";", comment.char="#")

}

我怎样才能避免空白,所以导入确实有效?还有其他建议吗? 非常感谢!

【问题讨论】:

  • 您还需要在 read.csv 函数中使用 paste 来创建一个代表您的文件名的字符串。所以 read.csv(paste("~/Test.-",Number,"File.csv", sep="", comment.char="#")。有更好的方法来使用 list.files() 和使用如果所有文件都在同一个文件夹中,则应用函数。为什么使用 sep=";" ??
  • sep=";"位于 *.csv 文件中的行之间。它们都在一个文件夹中!你有什么建议?谢谢!
  • 您能否只打印 .csv 中任意 1 的前两行或代表该格式的样本。 csv 文件不应有“;”在里面。
  • 如前所述:格式为 row1;row2;row3;... 并且由源设置。处理这些数据的问题在哪里?
  • 我认为你需要在 R 中读取文件之前清理文件并替换“;”使用 "" 或者您可以像这样直接读取文件: paste("Test.",Number,"File",sep=""))

标签: r loops csv text import


【解决方案1】:

@manaR 的 Anwer 解决了空白区域的问题。 stringrpackage str_c 也允许您这样做。您可以使用assign 在用户环境中创建一个具有正确名称的新变量。

require(stringr)
for (Number in 20:30){
        file<- read.csv(str_c("~/Test.-",Number,"File",".csv"), sep=";",comment.char="#")
        # assign value to the variable name x 
        assign(x=str_c("Test.",Number,"File"),value=file,envir=.GlobalEnv)  
      }



   # this example with the winprogressbar only works under windows
    # it allows you to load all the files with extension .csv in one directory


    datawd<-"c:/path/to/mydirectory/"
    listoffiles<-list.files(datawd) # list of files
    # find out which files have extensions csv
    listoffiles<-listoffiles[grep(".csv",listoffiles)]
    # creating the full path
    mypaths<-str_c(datawd,listoffiles)
    progress<-utils::winProgressBar(title = "loading csv files",
            label = "progression %",
            min = 0,
            max = length(mypaths), 
            initial = 0,
            width = 400)
    for (i in 1:length(mypaths)){
        utils::setWinProgressBar(progress,i,label=listoffiles[i])       
        file<- read.csv(mypaths, sep=";",comment.char="#")
        # assign value to the variable name x 
        assign(x=str_c("Test.",Number,"File"),value=file,envir=.GlobalEnv)  
    }

【讨论】:

  • 感谢完美,但我也对一个文件夹中提到的“更好的方法”感兴趣?社区中有任何链接吗?
  • @HerrStudent 查看 R 中的 list.files() 和 lapply() 这将为您提供一个列表对象,其中包含所有文件的数据作为列表中的 data.frames。
【解决方案2】:

你可以给paste()添加一个额外的参数

for (Number in 20:30){
  name <- paste("Test.",Number,"File.csv", sep = "")
  name <- read.csv(name, sep=";", comment.char="#")

}

sep="" 将删除名称中的空格。

【讨论】:

  • 如果您的文件是 csv 文件,则应该用逗号分隔,而不是 ; .所以这可能是另一个问题。
  • 文件的格式从源头上是固定的。用“,”或“;”分隔有什么区别订购“TAB”。我只需要在 read.csv 中设置值 - 谢谢!
  • 如果他们真的被;分隔,这很好,但正如@Tushar 所说, csv 不应该有 ;分隔符,而是逗号。毕竟它们被称为逗号分隔值= csv
  • 好的 - 这是一种行业定义:我知道它被称为“逗号分隔值” - 但实际上它是分号。
  • @HerrStudent 逗号分隔值表示列以“,”分隔,而行通常以“\n\r”或返回键分隔。
【解决方案3】:

试试这个:

for (number in 20:30){
    name<-paste("Test.-",number,"File",sep="")
    name<-read.csv(paste(filename,".csv",sep=""))
   }

默认情况下,如果您没有在 paste() 中指定 sep="",则 sep 设置为“”。 但仍然在最后一列中你会得到一个“;”您必须使用 gsub 函数替换它。

【讨论】:

    【解决方案4】:

    sep="," 中使用"," 帮助了我。感谢 manaR,塞德里克。

    for (i in 1:96){
      d <- read.csv(str_c("GRID_",Number,".csv"), sep=",",comment.char="#")
      source("Function_for grid.R")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 2021-10-18
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      相关资源
      最近更新 更多