Skip to content
Prev 360408 / 398503 Next

Using read.csv() to import data

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.