导入csv文件时列数错误

时间:2016-04-21 11:47:28

标签: r csv

我有17列和72920行的数据集。数据集从Excel保存为csv格式。 然后我将它加载到R 3.1.2:

old<-read.table(file.choose(), header=TRUE)
  

STR(旧)   'data.frame':72920 ob​​s。 1个变量:    $ OID.LO.MAG.PCR.WAT.SVE.ARA.GRA.FOR.MHF.B13.B12.MTP.BI6.BI4.ALT.BI1:因子w / 72920等级“1; 1; 0; 0; 0 ; 0; 0; 0; 0; 32.00; 100; 420; 43; 72; 4821; 18.18; 18.555“,..:1 11112 22223 33334 44445 55556 66667 70699 71810 2 ...

问题在于R将我的数据集视为1列而不是17列。 这是我的数据在导出之前的样子:

OID LO  MAG PCR WAT SVE ARA GRA FOR MHF B13 B12 MTP BI6 BI4 ALT BI1 URB
    1   1   0   0   0   0   0   0   0   32.00   100 420 43  72  4821    18.18   18.555  1
    2   1   0   1   0   0   0   0   0   39.17   99  421 81  72  4886    20.14   18.586  0
    3   1   0   0   0   0   1   0   0   29.25   112 474 13  74  4947    132.80  18.470  0
    4   1   0   0   0   0   0   1   0   35.98   114 485 4   70  4997    166.54  18.213  0
    5   1   0   0   0   0   0   0   0   39.21   104 438 3   74  4859    54.82   18.580  1
    6   1   0   0   0   0   0   1   0   40.45   109 454 3   68  4971    107.65  18.216  0
    7   1   0   0   0   0   0   1   0   41.81   107 435 2   69  4909    46.70   18.400  0
    8   1   0   0   0   0   1   0   0   49.78   105 427 21  70  5009    18.36   18.636  0
    9   1   1   0   0   0   0   0   0   53.00   106 427 98  70  5173    13.33   18.767  0

我也试过导出为txt,但是当我将数据作为表格导入时,我遇到了与上面相同的问题。 我希望有人能帮我正确地读取数据。

2 个答案:

答案 0 :(得分:2)

您应该指定一个分隔符,如下所示:

https://stat.ethz.ch/R-manual/R-devel/library/utils/html/read.table.html

old <- read.table(file, header = TRUE, sep = ";")

答案 1 :(得分:1)

我们需要使用$paths = transform_tree_to_paths($trees); function transform_tree_to_paths($trees, $current_path = '', $paths = array()) { if (is_array($trees)) { foreach ($trees as $tree => $children) { $current_path .= $tree . '/'; return transform_tree_to_paths($children, $current_path, $paths); } $paths[] = $current_path; $current_path = ''; } else { $paths[] = $trees; } return $paths; }

设置分隔符参数

可以找到有关sep = ";"(和read.table)的更多信息here

所以代码看起来像这样:

read.csv

或者您也可以使用old <- read.table(file.choose(), header = TRUE, sep = ";") 分隔(; )分隔文件,或read.table代替逗号(,< / strong>)分隔文件。在你的情况下,这将是这样的:

read.csv2()