Skip to content

Using read.csv() to import data

6 messages · Jason Hernandez, Duncan Murdoch, David L Carlson +2 more

#
I am just beginning to learn R, using _R for Dummies_ by Andrie de Vries and Joris Meys. I am using Windows 7, and RGui (64-bit) version 3.0.2. I have reached the chapter on "Getting Data Into and Out of R." But the code they use for importing data doesn't seem to be working for me.

Their example is:> elements <- read.csv(file.path("f:", "elements.csv"))
Since I don't have any such file, I am trying to use a file I have. I went to Excel, brought up my file titled JPH_data, and saved it as .csv (comma delineated) on my main hard drive C:

Then I entered:>? mammals <- read.csv(file.path("C:", "JPH_data.csv"))
I got the following:Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
? cannot open file 'C:/JPH_data.csv': No such file or directory
Aside from the obvious (how can it say "no such file or directory" when I just saved one such?), the "cannot open the connection" is also unexpected. What am I doing wrong here?
Jason Hernandezno current affiliation
#
On 24/04/2016 4:30 PM, Jason Hernandez via R-help wrote:
By far the easiest ways to enter Windows file paths are using the 
file.choose() and choose.files() functions.  Do something like

filename <- file.choose() # navigate to the file
mammals <- read.csv(filename)

and you should be fine.  The file.choose() function works on all 
platforms; choose.files() works only on Windows (and has more options, 
including allowing multiple files to be chosen).

Duncan Murdoch
#
You can also use getwd() to see what folder/directory R is currently using and setwd("folder") to change it. Also list.files() lists all of the files in that current directory. 

The code you included assumes that the file is located in the root directory which is probably wrong. Try 

mammals <- read.csv("JPH_data.csv")

If you are just starting out learning R, you can create a directory to use with the book by  using the dir.create() function:

dir.create("LearnR")
setwd("LearnR")

This will create a new folder in your default folder (probably called "My Documents"). Whenever you start R run setwd("LearnR") first so that anything you save goes in that folder. If you create .csv files, put them in that folder and R will find them easily. Also all of your R files will be in one place making them easier to find.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch
Sent: Sunday, April 24, 2016 4:05 PM
To: Jason Hernandez; r-help at r-project.org
Subject: Re: [R] Using read.csv() to import data
On 24/04/2016 4:30 PM, Jason Hernandez via R-help wrote:
By far the easiest ways to enter Windows file paths are using the 
file.choose() and choose.files() functions.  Do something like

filename <- file.choose() # navigate to the file
mammals <- read.csv(filename)

and you should be fine.  The file.choose() function works on all 
platforms; choose.files() works only on Windows (and has more options, 
including allowing multiple files to be chosen).

Duncan Murdoch

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
First save your file as csv and then type the following in the console....assignname
assignname=read.csv(file.choose())

This would open up your dcouments file and then you can select the file you saved as csv.
Cheers

      From: Jason Hernandez via R-help <r-help at r-project.org>
 To: "r-help at r-project.org" <r-help at r-project.org> 
 Sent: Sunday, April 24, 2016 1:30 PM
 Subject: [R] Using read.csv() to import data
   
I am just beginning to learn R, using _R for Dummies_ by Andrie de Vries and Joris Meys. I am using Windows 7, and RGui (64-bit) version 3.0.2. I have reached the chapter on "Getting Data Into and Out of R." But the code they use for importing data doesn't seem to be working for me.

Their example is:> elements <- read.csv(file.path("f:", "elements.csv"))
Since I don't have any such file, I am trying to use a file I have. I went to Excel, brought up my file titled JPH_data, and saved it as .csv (comma delineated) on my main hard drive C:

Then I entered:>? mammals <- read.csv(file.path("C:", "JPH_data.csv"))
I got the following:Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
? cannot open file 'C:/JPH_data.csv': No such file or directory
Aside from the obvious (how can it say "no such file or directory" when I just saved one such?), the "cannot open the connection" is also unexpected. What am I doing wrong here?
Jason Hernandezno current affiliation

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Duncan,
What about converting your anova results in R ?back into csv or excel?
Thanks
Charles ?

      From: Duncan Murdoch <murdoch.duncan at gmail.com>
 To: Jason Hernandez <jason.hernandez74 at yahoo.com>; "r-help at r-project.org" <r-help at r-project.org> 
 Sent: Sunday, April 24, 2016 2:05 PM
 Subject: Re: [R] Using read.csv() to import data
On 24/04/2016 4:30 PM, Jason Hernandez via R-help wrote:
By far the easiest ways to enter Windows file paths are using the 
file.choose() and choose.files() functions.? Do something like

filename <- file.choose() # navigate to the file
mammals <- read.csv(filename)

and you should be fine.? The file.choose() function works on all 
platforms; choose.files() works only on Windows (and has more options, 
including allowing multiple files to be chosen).

Duncan Murdoch

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Keep on reading.

R functions (including analyses and even plots in some cases) pproduce
objects with various structures that are then summarized and displayed
by other functions (which also produce objects), sometimes
automagically. To "export" results to files or other software you need
to know how to capture, manipulate, and extract from these objects.
Keep on studying to learn how. There are also many good online
tutorials. See here for some recommendations:
https://www.rstudio.com/online-learning/#R


Of course, many of us would question why you would want to export
results back to Excel, as R has far greater capabilities  (you can
even produce "dynamic" Word reports with R results using R and
appropriate packages).  But then again, we don't know your situation
... But if you can, in the long run you might be better off ditching
Excel and gaining facility in R. It *does* require some time and
effort, though.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Apr 25, 2016 at 7:53 PM, charles rockson via R-help
<r-help at r-project.org> wrote: