【问题标题】:Get filename from read.csv(file.choose( ))从 read.csv(file.choose()) 中获取文件名
【发布时间】:2012-02-10 19:15:29
【问题描述】:

我想知道是否可以从 read.csv 调用中嵌入的 file.choose() 命令中提取文件名。现在我分两步执行此操作,但用户必须选择同一文件两次才能提取数据(csv)和文件名以用于我运行的函数。我想让它让用户只需选择一次文件,然后我就可以使用数据和文件名。

这是我正在使用的:

data <- read.csv(file.choose(), skip=1))
name <- basename(file.choose())

如果有帮助,我正在运行 OS X,因为我认为 file.choose() 的行为取决于操作系统。提前致谢。

【问题讨论】:

    标签: r


    【解决方案1】:

    为什么要使用嵌入式file.choose() 命令?

    filename <- file.choose()
    data <- read.csv(filename, skip=1)
    name <- basename(filename)
    

    【讨论】:

    • @user1202761 如果你想嵌入它,data &lt;- read.csv(filename&lt;-file.choose(),skip=1)
    【解决方案2】:

    使用这个:

    df = read.csv(file.choose(), sep = "<use relevant seperator>", header = T, quote = "")
    

    分隔符一般为逗号,或句号.

    例子:

    df = read.csv(file.choose(), sep = ",", header = T, quote = "")
    
    #

    使用:

    df = df[,!apply(is.na(df), 2, all)] # works for every data format
    to remove blank columns to the left 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 2011-03-19
      相关资源
      最近更新 更多