R toolbox: open your data set in R with read.csv and the “here” package

I like to save my rawdata in csv format (from excel, Stata or other sources), which makes the access easy as well as it is a durable file format that most likely will be relatively unaffected by new versions of software and thus is a good file format for archiving your data.

Make sure that the csv actually is comma seperated, not tab seperated or semicolon seperated, cause R doesn´t really like those. If you happen to create semicolon seperated csv files from Excel, you probably need to change your region settings, as region and the csv format are linked in Excel, which is weird but true.

In R, to avoid problems with file paths when working on different computers, or switching from PC to Mac, use the “here” package. It is very simple and easy to use, but you need to make sure that you have organized your R/R studio project in the same folder hierarchy as your csv file.

require(here)

Then, open your csv in R using the code below. Notice that you do not need to specify the file path, if you have activated the here package before.

# Get the data
yourdata <- read.csv(file="mycsvfile.csv") 

Now your dataframe should appear in your environment, well done!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s