【问题标题】:Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 2 elements扫描错误(文件,内容,nmax,sep,dec,quote,skip,nlines,na.strings,:第 1 行没有 2 个元素
【发布时间】:2014-12-21 09:24:09
【问题描述】:
examdata <- RCurl::getURL("https://raw.githubusercontent.com/jrwolf/IT497/master/spendingdata.txt")

examdata2 <- read.table(textConnection(examdata), sep = ",", header = T)

扫描错误(文件、内容、nmax、sep、dec、quote、skip、nlines、 na.strings, : 第 1 行没有 2 个元素

【问题讨论】:

  • 试试examdata2 &lt;- read.table(textConnection(examdata), sep = ",", header = TRUE, skip=31, stringsAsFactors=FALSE)
  • 为什么要删除网址?问题的答案很重要

标签: r


【解决方案1】:

试试:

df <- read.csv("x.csv",... ,**quote = "", fill=TRUE**)

【讨论】:

    【解决方案2】:

    read.tableread.csv 将使用 URL 作为路径并为您处理连接,因此您实际上并不需要 RCurl

    read.csv("https://raw.githubusercontent.com/jrwolf/IT497/master/spendingdata.txt", 
             skip = 31)
    
    ##                          Type Cash Check Credit Debit Electronic Other Total
    ## 1 Average Number of Purchases 23.7   3.9   10.1  14.4        4.4   2.3  58.7
    ## 2   Average Transaction Value  $21  $168    $56   $44       $216   $69   $59
    

    此外,如果您使用readr::read_csv,您可以告诉它将列解析为数字,并在读取时去除$ 字符:

    library(readr)
    
    read_csv("https://raw.githubusercontent.com/jrwolf/IT497/master/spendingdata.txt", 
             skip = 31, 
             col_types = cols(Type = 'c', .default = 'n'))    # c = character, n = number
    
    ## # A tibble: 2 × 8
    ##                          Type  Cash Check Credit Debit Electronic Other Total
    ##                         <chr> <dbl> <dbl>  <dbl> <dbl>      <dbl> <dbl> <dbl>
    ## 1 Average Number of Purchases  23.7   3.9   10.1  14.4        4.4   2.3  58.7
    ## 2   Average Transaction Value  21.0 168.0   56.0  44.0      216.0  69.0  59.0
    

    【讨论】:

      【解决方案3】:

      看起来你只需要跳过几行。我使用readLines(textConnection(examdata)) 来确定实际数据表的开始位置。原来它从第 32 行开始。因此我们可以使用read.csv 中的skip 参数来跳过前31 行。我使用了strip.white 参数,因为表格中似乎有一些错误的空格。

      (df <- read.csv(text = examdata, skip = 31L, strip.white = TRUE))
      #                          Type Cash Check Credit Debit Electronic Other Total
      # 1 Average Number of Purchases 23.7   3.9   10.1  14.4        4.4   2.3  58.7
      # 2   Average Transaction Value  $21  $168    $56   $44       $216   $69   $59
      # 3      Value of Payments in %   14    19     16    18         27     5   100
      

      由于您可能希望这些数字为数字,因此您需要删除 $ 符号并将列转换为数字,以便您可以将它们用于以后可能进行的任何计算。

      df[-1] <- lapply(df[-1], function(x) as.numeric(sub("[$]", "", x)))
      df
      #                          Type Cash Check Credit Debit Electronic Other Total
      # 1 Average Number of Purchases 23.7   3.9   10.1  14.4        4.4   2.3  58.7
      # 2   Average Transaction Value 21.0 168.0   56.0  44.0      216.0  69.0  59.0
      # 3      Value of Payments in % 14.0  19.0   16.0  18.0       27.0   5.0 100.0
      

      现在除了第一列之外的所有列都是数字。

      【讨论】:

      • 感谢您的帮助......你只是给了我答案......你也可以解释这些东西。
      • 好吧,您可以在帖子中添加一些文字,而不是仅仅转储不起作用的代码
      猜你喜欢
      • 2013-10-27
      • 2015-05-11
      • 2021-12-22
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2019-09-12
      相关资源
      最近更新 更多