| by Arround The Web | No comments

How to Import Data into R from CSV

As we know, a CSV file makes it possible to store material in a tabular-like structure that is arranged in a series of rows and columns. We can use the CSV file in the R environment to perform read and write operations. R provides a built-in method and external packages to import the data from CSV. Here, we will cover most of the ways to quickly get the CSV data into the R environment using the R function and packages. Before that, we must create the CSV file and add the data accordingly.

Data Stored in the CSV File:

The CSV file that we are using to import in R is represented below with the specified data.

Example # 1: Import Data Into R From CSV Using the read.csv() Method

The first standard method for importing CSV file data in R is the read.csv() method.

We have defined the variable called “empData,” to which the data from the CSV file is imported. To do this, we used the read.csv() method and passed the name of the CSV file “emp_data.csv”. After this, we have the print() function, which takes the variable “empData” where the imported CSV file data is stored. The CSV file’s contents will be fetched, and the entire data set will be displayed on the R interface.

empData <- read.csv("emp_data.csv")
print(empData)

Hence, the CSV file data has been imported successfully using the read.csv() method on the prompt.

Example # 2: Import Data Columns and Rows into R from CSV

In the previous illustration, the data was imported from the CSV file. Now, we can also get the number of exact columns and rows from that file, which is implemented below.

We have a variable “d1” where the read.csv() method is defined with an argument “emp_data.csv”. The “d1” variable stored the CSV file data with each column and row. Next, we called the print() function, where the is.data.frame() function is deployed to examine whether the variable “d1” passed as an argument stored in the data type is the data frame. Then, the results are based on the Boolean value “TRUE” or “FALSE.” After that, we printed the number of rows and columns by using the ncol() and nrow() methods, respectively.

d1 <- read.csv("emp_data.csv")
print(is.data.frame(d1))
print(ncol(d1))
print(nrow(d1))

Thus, the output first represents the “TRUE” value, indicating the specified data type is a data frame. Then, it displays the total number of total columns and rows in the given data.

Example # 3: Import Data into R from CSV Using the read_csv() Method

Sometimes, the CSV file contains a large dataset, which is difficult to import in R. For this, R provided the read_csv() method for importing the larger CSV file.

library(readr)
d2 <- read_csv("emp_data.csv")
str(d2)

We have imported the R library “readr,” which is an efficient and user-friendly method for extracting rectangular data (such as “csv”, “tsv” and “fwf”). After that, we created the “d2” variable, where the data in “emp_data.csv” is imported using the read_csv() method. Then, we used the str() function and passed “d2” inside it to view the structure of the CSV data in R.

The output shows the column specifications below after executing the read_csv() method and then the structure of the imported data in tibbles.

Example # 4: Import Data into R from CSV Using the fread() Method

Another way to import the larger CSV data in R is using the fread() method. The fread() method is the fastest and most efficient method to import extremely large CSV data in R.

library(data.table)
MyData <- fread("emp_data.csv")
head(MyData)
str(MyData)

We have included the data.table library first, which works with the tabular dataset in R. Then, we define the “MyData” variable, which is assigned with the fread() method of R to import the “emp_data.csv” file data. The “MyData” is then passed to the head() method to only show the top rows of the imported data if the CSV file contains a larger dataset. Finally, the str() function takes “MyData” as a parameter to display the structure of the imported dataset.

The structure of the imported data from the specified CSV has been displayed in the output.

Example # 5: Import Data into R from CSV Using read.csv() Method with sep Argument

However, we can also read the CSV file with the custom delimiter using a sep argument, as the read.csv() function uses commas as delimiters.

read_file = read.csv('emp_data.csv',sep=',')
print(read_file)

We have a “read_file” variable that deploys the read.csv() method. The read.csv() method takes the “emp_data.csv” file to import the data in R. Along with that, we have specified the “sep” argument, which is set with the comma delimiter “,” to read the CSV file separated by the specified delimiter. Then, we print the imported data here by specifying the “read_file” to the print() function.

The CSV data has been imported and displayed in the output.

Example # 6: Import Data into R from CSV Using read.csv() Method with na.string Argument

There’s a chance that the bigger CSV file has some blank or missing values. Here, na.string() is used as a parameter in the read.csv() file, which considers all the empty and missing values as NA.

ReadCsv = read.csv('emp_data.csv',na.strings=c(-1,''))
print(ReadCsv)

We have a variable “ReadCsv” for which the read.csv() method is specified. Here, we have passed “emp_data.csv” as the first argument and na.strings=c(-1,’’) as the second argument. The na.strings=c(-1,’’) argument substitutes the string with NA in the file data when they match. Lastly, we print the imported CSV data and update the empty value with NA.

The imported data from the CSV file is shown below with the NA value where the values are empty.

Example # 7: Import Specific Data into R from CSV by Applying the Condition

The specific data in CSV can also be imported into R if the specified condition is satisfied. Below, we have applied the criteria for the CSV file to import the matched data.

emp_data <- read.csv("emp_data.csv")
result <- subset( emp_data, dept == "HR")
print(result)

We have set the variable “emp_data” and passed the read.csv() method to it. Here, we have specified the CSV file “emp_data.csv” to import the data. After that, we have set the result variable where the subset() function is called to select the filter and defined “emp_data” as the first parameter, and the second parameter is the filter that is used to match the column “dept” from the specified CSV file with the value “HR”.

The only data is imported in the output from the CSV, which fulfills the given criteria.

Conclusion

We have covered numerous ways to import data in R from CSV files by implementing the examples. The approaches are easy to implement and supported by the R language. Importing the data in R from CSV is crucial in data manipulation or analysis since the data must be available in R in order to be processed.

Share Button

Source: linuxhint.com

Leave a Reply