How to Read a .csv File in R

R - CSV Files


In R, we tin read data from files stored exterior the R environment. We can as well write data into files which will be stored and accessed by the operating system. R tin read and write into various file formats similar csv, excel, xml etc.

In this chapter we will acquire to read data from a csv file and then write data into a csv file. The file should be present in current working directory so that R can read information technology. Of class we can besides prepare our ain directory and read files from there.

Getting and Setting the Working Directory

You can check which directory the R workspace is pointing to using the getwd() function. Y'all can also set up a new working directory using setwd()function.

# Become and print current working directory. print(getwd())  # Set current working directory. setwd("/web/com")  # Get and impress current working directory. print(getwd())        

When we execute the in a higher place code, information technology produces the following event −

[1] "/spider web/com/1441086124_2016" [ane] "/web/com"        

This result depends on your OS and your current directory where you are working.

Input every bit CSV File

The csv file is a text file in which the values in the columns are separated past a comma. Let's consider the post-obit information present in the file named input.csv.

You can create this file using windows notepad by copying and pasting this data. Salve the file every bit input.csv using the save Every bit All files(*.*) choice in notepad.

id,proper name,bacon,start_date,dept one,Rick,623.iii,2012-01-01,IT 2,Dan,515.two,2013-09-23,Operations iii,Michelle,611,2014-11-15,IT 4,Ryan,729,2014-05-11,HR v,Gary,843.25,2015-03-27,Finance 6,Nina,578,2013-05-21,It 7,Simon,632.8,2013-07-30,Operations 8,Guru,722.5,2014-06-17,Finance        

Reading a CSV File

Following is a simple instance of read.csv() function to read a CSV file available in your current working directory −

information <- read.csv("input.csv") print(data)        

When nosotros execute the above code, it produces the post-obit result −

          id,   name,    salary,   start_date,     dept 1      1    Rick     623.30    2012-01-01      Information technology 2      ii    Dan      515.20    2013-09-23      Operations three      three    Michelle 611.00    2014-11-15      It 4      iv    Ryan     729.00    2014-05-eleven      HR 5     NA    Gary     843.25    2015-03-27      Finance six      6    Nina     578.00    2013-05-21      IT 7      7    Simon    632.80    2013-07-thirty      Operations 8      8    Guru     722.l    2014-06-17      Finance        

Analyzing the CSV File

By default the read.csv() function gives the output as a data frame. This can be easily checked equally follows. Also nosotros tin can check the number of columns and rows.

data <- read.csv("input.csv")  impress(is.data.frame(information)) print(ncol(information)) print(nrow(data))        

When we execute the to a higher place lawmaking, it produces the following event −

[1] TRUE [1] 5 [i] 8        

Once we read data in a data frame, we tin apply all the functions applicative to data frames as explained in subsequent department.

Get the maximum salary

# Create a data frame. information <- read.csv("input.csv")  # Get the max salary from information frame. sal <- max(data$salary) impress(sal)        

When we execute the in a higher place code, it produces the post-obit result −

[i] 843.25        

Become the details of the person with max bacon

We can fetch rows meeting specific filter criteria similar to a SQL where clause.

# Create a information frame. data <- read.csv("input.csv")  # Get the max salary from data frame. sal <- max(data$salary)  # Get the person item having max salary. retval <- subset(data, bacon == max(salary)) impress(retval)        

When we execute the above code, it produces the following result −

          id    name  salary  start_date    dept 5     NA    Gary  843.25  2015-03-27    Finance        

Get all the people working in Information technology department

# Create a data frame. data <- read.csv("input.csv")  retval <- subset( information, dept == "IT") impress(retval)        

When nosotros execute the higher up code, information technology produces the following result −

          id   name      salary   start_date   dept i      i    Rick      623.3    2012-01-01   IT iii      3    Michelle  611.0    2014-11-15   IT half dozen      6    Nina      578.0    2013-05-21   IT        

Get the persons in IT section whose salary is greater than 600

# Create a data frame. data <- read.csv("input.csv")  info <- subset(data, salary > 600 & dept == "IT") print(info)        

When nosotros execute the above lawmaking, information technology produces the following result −

          id   name      salary   start_date   dept i      1    Rick      623.3    2012-01-01   Information technology 3      3    Michelle  611.0    2014-xi-15   It        

Get the people who joined on or after 2014

# Create a information frame. information <- read.csv("input.csv")  retval <- subset(data, as.Date(start_date) > as.Appointment("2014-01-01")) impress(retval)        

When we execute the above lawmaking, it produces the following result −

          id   name     salary   start_date    dept 3      3    Michelle 611.00   2014-11-xv    Information technology 4      4    Ryan     729.00   2014-05-11    HR 5     NA    Gary     843.25   2015-03-27    Finance 8      8    Guru     722.50   2014-06-17    Finance        

Writing into a CSV File

R can create csv file class existing information frame. The write.csv() function is used to create the csv file. This file gets created in the working directory.

# Create a data frame. information <- read.csv("input.csv") retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))  # Write filtered data into a new file. write.csv(retval,"output.csv") newdata <- read.csv("output.csv") print(newdata)        

When we execute the above lawmaking, it produces the post-obit consequence −

          X      id   proper noun      salary   start_date    dept 1 3      3    Michelle  611.00   2014-11-fifteen    IT 2 iv      4    Ryan      729.00   2014-05-eleven    HR three five     NA    Gary      843.25   2015-03-27    Finance 4 8      8    Guru      722.50   2014-06-17    Finance        

Here the cavalcade X comes from the data set up newper. This can exist dropped using additional parameters while writing the file.

# Create a information frame. information <- read.csv("input.csv") retval <- subset(data, as.Appointment(start_date) > as.Date("2014-01-01"))  # Write filtered information into a new file. write.csv(retval,"output.csv", row.names = FALSE) newdata <- read.csv("output.csv") print(newdata)        

When nosotros execute the above lawmaking, it produces the following result −

          id    name      salary   start_date    dept 1      3    Michelle  611.00   2014-11-15    It ii      iv    Ryan      729.00   2014-05-11    Hr 3     NA    Gary      843.25   2015-03-27    Finance iv      8    Guru      722.fifty   2014-06-17    Finance        

Useful Video Courses


JCL Online Training

Video

DB2 Online Training

Video

COBOL Online Training

Video

Email Marketing Online Training

Video

Mainframe Online Training

Video

CRO Online Training

Video

thomasharrinat.blogspot.com

Source: https://www.tutorialspoint.com/r/r_csv_files.htm

0 Response to "How to Read a .csv File in R"

Yorum Gönder

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel