CodeInfoNet

Learn programming on the go!

How to import a .Csv file in R

2. Get/Set working wirectory

In order to work with this, we need to set the working directory same as the location of file to be imported

This is another way to import a csv file in R. For using this, we first need to make sure if we know the current working directory or not.

getwd()

To know the working directory, execute function getwd()

Now either you can copy your file to that location or you can set the working directory to your choice of location. It’s up to you!

setwd()

To set the preferred location as current working directory, execute the function setwd() by providing appropriate path to it.

Setwd() expects parameters in different ways on windows system and on mac system. This is another reason why this method can’t be preferred. So, the syntax for the same is as below:

Windows: setwd(“C:\\CodeinfoNet\\Repositories\\R”)

Mac: setwd(“C:/CodeinfoNet/Repositories/R”)

As we have now set the working directory, we can just specify the file name of a file that we want to import.

read.csv(“SampleData.csv”)

 Now to store the data from imported file, we will use similar approach as mentioned above.

Data2 <- read.csv(“SampleData.csv”) This is it. We imported csv file successfully.

In this article we learnt 3 functions:

  1. getwd()
  2. setwd()
  3. read.csv()

I hope you guys enjoyed this read.

Code for this article is also available on CodeInfoNet’s git repository. Check it out and try. Here is the url: Importing .csv file in R Git Code

Keep us posted if you have any query. Your advice is priceless. Please provide your feedback regarding the quality of explanation or the complexity of content (if any) using below feedback form.

Feel free to comment or reach us directly via mail. Also you can contact us by Facebook, linkedin, twitter, instagram any of the platform you feel comfortable with.

Keep Learning. Keep Coding.

Sumit Rajguru

Back to top