DEV Community

Mahesh K
Mahesh K

Posted on • Updated on

R Language : Read CSV files In R

Let's discuss how to read CSV files from R. Most of the office users still depend on CSV files and Excel files for storing the raw data. Though a lot of people have moved to the cloud storage of different types.

But the export options in most of the cloud data collection platforms often have CSV file as an option. So we are likely to encounter a lot of CSV files when we wish to read data as a R programmer.

So here we are going to take see simple tutorial to achieve this task. If you wish to watch the video instruction of this tutorial, check out How to Read CSV files in R.

Let's assume that we have file named mydata.csv. This has data something like this.

Item,Cost,Sold,Profit
Keyboard,$10.00,$16.00,$6.00
Monitor,$80.00,$120.00,$40.00
Mouse,$5.00,$7.00,$2.00
Enter fullscreen mode Exit fullscreen mode

Now that we have some sample file to deal with, let's try to read this file with R. In R we can make use of the read.csv() function and read our file so it looks something like this.

dt = read.csv("mydata.csv")
Enter fullscreen mode Exit fullscreen mode

To output the data on the console you can simply call the dt now.

dt
Enter fullscreen mode Exit fullscreen mode

This should produce the output that looks something like this.

Item,Cost,Sold,Profit
Keyboard,$10.00,$16.00,$6.00
Monitor,$80.00,$120.00,$40.00
Mouse,$5.00,$7.00,$2.00
Enter fullscreen mode Exit fullscreen mode

You can also use the head() function with the dt and try to get much better view of the data that is properly spaced.

That's it. This is pretty simple way to read the CSV files. There are few other functions too that can help you manipulate and write the new CSV file which I will cover in another post.

I hope this helps you with reading the CSV file for your data.

Top comments (0)