Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unrecognized escape in character string" while attempting to read a CSV file

Tags:

import

r

I am trying to import a .csv file, so that I can follow along with this video: R ggplot2 Graphics Histograms.

I installed all proper packages including ggplot and related packages. The first instruction in the video says to type afl.df=read.csv("afl_2003_2007.csv")

So, I downloaded afl_2003_2007.csv file, and I tried all the below, which was basically putting the file in different directories (shared drive, then C drive, etc.). I also tried using setwd, but no luck.

I am using R in windows.

Here's what I tried, and the errors I got:

> afl.df=read.csv("afl_2003_2007.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'afl_2003_2007.csv': No such file or directory
> afl.df=read.csv("\\the-lab.llnl.gov\llnlusers1\lopez235\Data\Documents\Dashboards,HRBI, Visulizations and Analytics\Math and Statistics and Predictive Modeling1\R Programming\afl_2003_2007.csv")
Error: '\l' is an unrecognized escape in character string starting "\\the-lab.llnl.gov\l"
> afl.df=read.csv("C:\Users\lopez235\Local-NOTBackedUp\R Files Local\afl_2003_2007.csv")
Error: '\U' used without hex digits in character string starting "C:\U"
> setwd("\\the-lab.llnl.gov\llnlusers1\lopez235\Data\Documents\Dashboards,HRBI, Visulizations and Analytics\Math and Statistics and Predictive Modeling1\R Programming\afl_2003_2007.csv")
Error: '\l' is an unrecognized escape in character string starting "\\the-lab.llnl.gov\l"
> setwd("\\the-lab.llnl.gov\llnlusers1\lopez235\Data\Documents\Dashboards,HRBI, Visulizations and Analytics\Math and Statistics and Predictive Modeling1\R Programming")
Error: '\l' is an unrecognized escape in character string starting "\\the-lab.llnl.gov\l"
> setwd("C:\Users\lopez235\Local-NOTBackedUp\R Files Local")
Error: '\U' used without hex digits in character string starting "C:\U"
like image 472
daniellopez46 Avatar asked May 02 '12 16:05

daniellopez46


2 Answers

Use / instead of \ in your path:

afl.df=read.csv("C:/Users/lopez235/Local-NOTBackedUp/R Files Local/afl_2003_2007.csv")
like image 181
David Robinson Avatar answered Jan 03 '23 18:01

David Robinson


When encountering issues with importing datasets I prefer to use file.choose() and then pick my file manually. For example :

newdataset <- read.csv(file.choose(), header = T)

a window asking you to selext your file manually will pop-up and header = T (or TRUE) tells R that these are the variable names. If you have data write header = FALSE. If you want to confirm that now R knows which are the variable names you can call: names(newdataset)

like image 43
Nathalie Vladis Avatar answered Jan 03 '23 17:01

Nathalie Vladis