R-Import a CSV Dataset as a DataFrame using read.csv()

While learning R, we want to practice with some real data. To practice on some real data, we need some real data set. I googled and got the famous Titanic data set from Kaggle. I downloaded the “train.csv”data file and renamed it as “Titanic.csv”.While R can read excel .xls and .xlsx files, comma separated files (.csv) are much easier to work with and so I started from there. The next thing I asked is “How to import this data set into R”. 

One way of doing it is using the RStudio features to “Import Dataset”. The steps I have mentioned here. But, while learning R, it feels great to use more of R than a tool. So, here are the steps to import a dataset in R.


Problem:

Import a Data Set as a Data Frame using R 

Solution:
The utils package, which is automatically loaded in the R session on startup, can import CSV files with the read.csv() function. In the “Packages” Section, we can see the packages that are already loaded. See here:

To import the data in R, we can use the below code:

I ran the above piece of code in RStudio and a new file named “titanic” is available in R. The above code reads the file “titanic.csv” into a data frame called “titanic”.We can also add the arguments like ‘header=TRUE’ which specifies that this data includes a header row and sep=”,” specifies that the data is separated by commas. Though read.csv implies the same, it is safer to be explicit.

What are the other options or arguments that can be used with read.csv? So, to know more about it, we can click on help in RStudio and type”read.csv” and we can see the details.

 We got the data. Now, what next?

Now, we need to clean the data and run some plots to get a visual understanding. If you are wondering what is your next step, please check the below posts:

  1. Assignment on Data Manipulation in R
  2. ggplot2::Boxplot in R using Titanic Dataset
  3. ggplot2::Scatter Plot in R using Titanic Dataset
  4. Learning Logistic Regression in R 
  5. Random Forest Algorithm in R

Happy learning and Happy Coding!

0

Leave a Reply

Your email address will not be published. Required fields are marked *