Read in 3 Data Files in R

R - CSV Files


In R, we can read data from files stored exterior the R environment. We can also write data into files which will be stored and accessed by the operating organization. R can read and write into diverse file formats like csv, excel, xml etc.

In this chapter we will larn to read data from a csv file and so write data into a csv file. The file should be present in current working directory so that R can read it. Of course we can too set our own directory and read files from at that place.

Getting and Setting the Working Directory

You tin can check which directory the R workspace is pointing to using the getwd() part. You lot tin can also set a new working directory using setwd()office.

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

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

[one] "/web/com/1441086124_2016" [1] "/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 by a comma. Let's consider the following data present in the file named input.csv.

Y'all can create this file using windows notepad past copying and pasting this data. Save the file as input.csv using the save Every bit All files(*.*) option in notepad.

id,name,salary,start_date,dept one,Rick,623.3,2012-01-01,IT 2,Dan,515.2,2013-09-23,Operations iii,Michelle,611,2014-11-xv,IT 4,Ryan,729,2014-05-xi,HR 5,Gary,843.25,2015-03-27,Finance 6,Nina,578,2013-05-21,IT 7,Simon,632.8,2013-07-30,Operations viii,Guru,722.five,2014-06-17,Finance        

Reading a CSV File

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

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

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

          id,   proper name,    salary,   start_date,     dept ane      ane    Rick     623.30    2012-01-01      Information technology 2      ii    Dan      515.20    2013-09-23      Operations iii      iii    Michelle 611.00    2014-11-15      IT 4      4    Ryan     729.00    2014-05-11      HR 5     NA    Gary     843.25    2015-03-27      Finance half-dozen      half dozen    Nina     578.00    2013-05-21      IT 7      7    Simon    632.80    2013-07-thirty      Operations viii      8    Guru     722.50    2014-06-17      Finance        

Analyzing the CSV File

By default the read.csv() role gives the output as a information frame. This can be easily checked as follows. Also we can bank check the number of columns and rows.

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

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

[1] Truthful [one] 5 [1] eight        

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

Become the maximum bacon

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

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

[i] 843.25        

Go the details of the person with max salary

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

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

When we execute the higher up code, information technology produces the following outcome −

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

Become all the people working in Information technology section

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

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

          id   name      salary   start_date   dept one      ane    Rick      623.3    2012-01-01   IT 3      3    Michelle  611.0    2014-11-15   It 6      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 we execute the above code, it produces the post-obit upshot −

          id   name      salary   start_date   dept 1      1    Rick      623.3    2012-01-01   Information technology 3      iii    Michelle  611.0    2014-11-15   IT        

Become the people who joined on or after 2014

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

When we execute the to a higher place code, it produces the post-obit upshot −

          id   name     salary   start_date    dept three      three    Michelle 611.00   2014-11-15    Information technology 4      4    Ryan     729.00   2014-05-11    60 minutes 5     NA    Gary     843.25   2015-03-27    Finance viii      8    Guru     722.50   2014-06-17    Finance        

Writing into a CSV File

R can create csv file grade existing data frame. The write.csv() office 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, as.Engagement(start_date) > as.Date("2014-01-01"))  # Write filtered information into a new file. write.csv(retval,"output.csv") newdata <- read.csv("output.csv") impress(newdata)        

When nosotros execute the above code, information technology produces the following upshot −

          X      id   name      salary   start_date    dept 1 3      3    Michelle  611.00   2014-11-15    It 2 4      iv    Ryan      729.00   2014-05-11    60 minutes 3 v     NA    Gary      843.25   2015-03-27    Finance four 8      viii    Guru      722.50   2014-06-17    Finance        

Hither the cavalcade X comes from the data set newper. This tin be dropped using additional parameters while writing the file.

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

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

          id    name      bacon   start_date    dept 1      3    Michelle  611.00   2014-eleven-15    IT 2      4    Ryan      729.00   2014-05-eleven    60 minutes three     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

leetheareesum.blogspot.com

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

0 Response to "Read in 3 Data Files in R"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel