在R中使用read.csv时,列名未被正确读取。

6
我有一个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

1
无法复制您在此提供的数据:mycsvfile = read.csv(text = 'col1,col2\nhello,world\nfirst,last\nbye,bye'); names(mycsvfile); # [1] "col1" "col2" - lukeA
1个回答

8

这是一个编码问题:

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

应该可以工作。

我遇到了同样的问题!它在?read.csv中有记录,重定向到?fileencoding部分。 - Colonel Beauvel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接