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
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”,…..)
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.
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.
The read_csv function imports data into R as a tibble, while read. csv imports a regular old R data frame instead.
It is a problem with the encoding:
read.csv('C:\\Users\\wdsuser\\Desktop\\mycsvfile.csv', fileEncoding="UTF-8-BOM")
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With