How to Read a Csv File R

R - CSV Files


In R, we tin read data from files stored outside the R surroundings. We can too write data into files which will be stored and accessed past the operating system. R tin read and write into various file formats similar csv, excel, xml etc.

In this affiliate we will learn to read data from a csv file and and so write data into a csv file. The file should be present in electric current working directory so that R can read information technology. Of course we tin too set our own directory and read files from in that location.

Getting and Setting the Working Directory

You tin cheque which directory the R workspace is pointing to using the getwd() office. Yous can likewise set a new working directory using setwd()role.

# Get and print current working directory. print(getwd())  # Prepare current working directory. setwd("/spider web/com")  # Get and print electric current working directory. print(getwd())        

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

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

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

Input as CSV File

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

You can create this file using windows notepad by copying and pasting this information. Save the file every bit input.csv using the salve As All files(*.*) pick in notepad.

id,name,salary,start_date,dept 1,Rick,623.3,2012-01-01,IT 2,Dan,515.2,2013-09-23,Operations 3,Michelle,611,2014-xi-15,Information technology 4,Ryan,729,2014-05-xi,60 minutes 5,Gary,843.25,2015-03-27,Finance six,Nina,578,2013-05-21,Information technology seven,Simon,632.8,2013-07-xxx,Operations 8,Guru,722.5,2014-06-17,Finance        

Reading a CSV File

Following is a unproblematic example of read.csv() part to read a CSV file available in your current working directory −

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

When nosotros execute the above code, it produces the following issue −

          id,   name,    salary,   start_date,     dept one      1    Rick     623.30    2012-01-01      Information technology 2      ii    Dan      515.20    2013-09-23      Operations 3      3    Michelle 611.00    2014-eleven-fifteen      Information technology iv      iv    Ryan     729.00    2014-05-11      HR 5     NA    Gary     843.25    2015-03-27      Finance 6      6    Nina     578.00    2013-05-21      IT vii      7    Simon    632.80    2013-07-30      Operations viii      8    Guru     722.50    2014-06-17      Finance        

Analyzing the CSV File

Past default the read.csv() function gives the output every bit a information frame. This tin can be easily checked as follows. Too we can bank check the number of columns and rows.

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

When we execute the higher up code, it produces the post-obit result −

[1] Truthful [1] five [1] eight        

Once we read data in a information frame, nosotros can utilise all the functions applicable to information frames as explained in subsequent section.

Get the maximum salary

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

When we execute the higher up lawmaking, it produces the post-obit result −

[1] 843.25        

Get the details of the person with max bacon

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

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

When we execute the above code, it produces the post-obit issue −

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

Get all the people working in It department

# Create a data frame. data <- read.csv("input.csv")  retval <- subset( data, dept == "It") print(retval)        

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

          id   name      salary   start_date   dept 1      1    Rick      623.3    2012-01-01   It three      3    Michelle  611.0    2014-11-xv   IT half dozen      6    Nina      578.0    2013-05-21   Information technology        

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

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

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

          id   proper name      salary   start_date   dept one      one    Rick      623.3    2012-01-01   It 3      3    Michelle  611.0    2014-11-15   IT        

Go the people who joined on or after 2014

# Create a data frame. data <- read.csv("input.csv")  retval <- subset(data, equally.Engagement(start_date) > every bit.Date("2014-01-01")) print(retval)        

When we execute the in a higher place code, it produces the following issue −

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

Writing into a CSV File

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

# Create a data frame. data <- read.csv("input.csv") retval <- subset(information, every bit.Date(start_date) > every bit.Appointment("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 code, it produces the following effect −

          Ten      id   name      salary   start_date    dept 1 3      3    Michelle  611.00   2014-eleven-15    Information technology 2 4      4    Ryan      729.00   2014-05-11    Hour 3 5     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 newper. This tin be dropped using additional parameters while writing the file.

# Create a information frame. data <- 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", row.names = Faux) newdata <- read.csv("output.csv") impress(newdata)        

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

          id    name      salary   start_date    dept 1      3    Michelle  611.00   2014-eleven-fifteen    It 2      4    Ryan      729.00   2014-05-11    Hr iii     NA    Gary      843.25   2015-03-27    Finance 4      8    Guru      722.50   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

irizarryciary1949.blogspot.com

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

0 Response to "How to Read a Csv File R"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel