【问题标题】:columns names not read properly by read.csv in RR中的read.csv无法正确读取列名
【发布时间】:2015-01-17 23:48:06
【问题描述】:

我有一个 csv 文件,我试图将其内容读入 R。但我的列名没有正确显示,它在第一列的名称中显示了一些奇怪的字符。(请注意,这是我尝试读取的任何 csv 文件的情况)

请看下面的R代码

> mycsvfile = read.csv('C:\\Users\\wdsuser\\Desktop\\mycsvfile.csv')
> mycsvfile
  ï..col1  col2
1   hello world
2   first  last
3     bye   bye
> names(mycsvfile)
[1] "ï..col1" "col2"   
> mycsvfile$col2
[1] world last  bye  
Levels: bye last world
> mycsvfile$col1
NULL

这就是我的文本文件的样子

col1,col2
hello,world
first,last
bye,bye

R 版本:

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          1.2                         
year           2014                        
month          10                          
day            31                          
svn rev        66913                       
language       R                           
version.string R version 3.1.2 (2014-10-31)
nickname       Pumpkin Helmet

【问题讨论】:

  • 无法用您在此处提供的数据重现:mycsvfile = read.csv(text = 'col1,col2\nhello,world\nfirst,last\nbye,bye'); names(mycsvfile); # [1] "col1" "col2"

标签: r


【解决方案1】:

编码有问题:

read.csv('C:\\Users\\wdsuser\\Desktop\\mycsvfile.csv', fileEncoding="UTF-8-BOM")

应该可以。

【讨论】:

  • 我也遇到了同样的问题!它记录在 ?read.csv 中,重定向到 ?file 部分 encoding
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 2014-11-04
相关资源
最近更新 更多