Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

columns names not read properly by read.csv in R

Tags:

r

I have a csv file whose contents I'm trying to read into R. But my columns names don't get displayed correctly, it shows some weird characters in the name of the first columns.(Please note that this is the case with any csv file that I try to read in)

Please see R code below

> 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

This is how my text file looks

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

R version :

> 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
like image 204
IAMTubby Avatar asked Jan 17 '15 23:01

IAMTubby


People also ask

How do I add a column name to a csv file in R?

Method 1: Using colnames() function colnames() function in R is used to set headers or names to columns of a dataframe or matrix. Syntax: colnames(dataframe) <- c(“col_name-1”, “col_name-2”, “col_name-3”, “col_name-4”,…..)

How do I read a column from a csv file in R?

Method 1: Using read. table() function. In this method of only importing the selected columns of the CSV file data, the user needs to call the read. table() function, which is an in-built function of R programming language, and then passes the selected column in its arguments to import particular columns from the data.

How do I read a column in a csv file?

Use pandas. read_csv() to read a specific column from a CSV file. To read a CSV file, call pd. read_csv(file_name, usecols=cols_list) with file_name as the name of the CSV file, delimiter as the delimiter, and cols_list as the list of specific columns to read from the CSV file.

What is the difference between read csv and read_csv in R?

The read_csv function imports data into R as a tibble, while read. csv imports a regular old R data frame instead.


1 Answers

It is a problem with the encoding:

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

should work.

like image 141
Colonel Beauvel Avatar answered Nov 12 '22 09:11

Colonel Beauvel