R CSV Files - Read, Process & Write CSV files in R language - Tutorial Kart

[Pages:6]R CSV Files ? Read, Process & Write CSV files in R language

R provides functions to read and write to various file formats. In this R Tutorial, we shall look specifically into CSV Files.

R CSV Files

We shall learn R functions to :

R Read CSV Files R Process CSV Files R Write CSV Files

Example of a CSV File, that we use in the examples going forward, is given below :

s a m p l e C SV .cs v

Andrew,28,25.2 Mathew,23,10.5 Dany,49,11 Philip,29,21.9 John,38,44 BAinngd,r2e3w,1,12.85,25.2 MMoantichae,2w9,,2435,10.5 Dany,49,11 Philip,29,21.9 John,38,44 Bing,23,11.5 Monica,29,45

You may refer R Working Directory to modify the path of R Workspace to point to the directory containing your input files (CSV Files).

Read CSV Files

CSV Files are those files with values separated by commas in each row. Each row corresponds to a record or observation.

Syntax of function to read CSV File in R programming language :

re a d .cs v(< f i l e n a m e > ) Example to read CSV File in R programming language :

r_readCSVexample.R - R Script File

# Example R program to read CSV File

#set working directory - the d#ireEcxtoarmy pcolentRainpinrgogram to read CSV File csv file setwd("/home/arjun/ w#osrekstpwacoer/kr"in) g directory - the directory containing csv file

#sreetawddc(s"v/hfiloem e /a rj u n /w o rks p a ce /r") csvData = read.csv("sampleCSV. c#svre")ad csv file csvData = read.csv("sampleCSV.csv") # print the data type of csvData c#at(p"rCiSnVt tDhaetadtaytpae :type of csvData ",class(csvData), "c\an\tn(")CSV Data type : ",class(csvData), "\n\n")

print(csvData) p ri n t (cs vD a t a )

Terminal Output

$ Rscript r_readCSVexample.R CSV Data type : data.frame

$ Rnasmcreipatger_inrecoamdCeSVexample.R

1CASnVdDreawta28typ2e5.:2data.frame 2 Adarsh 23 10.5

3 Dany 49 11.0

4 5

PhJnoiliahpmn 2e398ag2e414.i9.n0come

61 ABnindgre2w3 28112.5.2

72MAodnaicrash2923 4150.0.5

3 Dany 49 11.0

4 Philip 29 21.9

5 John 38 44.0

6 Bing 23 11.5

7 Monica 29 45.0

Please observe that the data of csv file is read to an R Data Frame.

Process data read from CSV Files

R programming language reads the CSV File to an R Dataframe. So, you may use all theR Data Frame functions to process the data. Some of the examples are given below.

Example R program to retrieve rows based on a condition applied to column

From the csv file, we shall extract rows, whose income is equal to the maximum of income.

r_csv_analyze_data.R - R Script File

# Example R program to analyze CSV File

#set working directory - the d#ireEcxtoarmy pcolentRainpinrgogram to analyze CSV File csv file setwd("/home/arjun/ w#osrekstpwacoer/kr"in) g directory - the directory containing csv file #sreetawddc(s"v/hfiloem e /a rj u n /w o rks p a ce /r") celebrities = read.csv("sampleCSV. c#svre")ad csv file celebrities = read.csv("sampleCSV.csv") # retrieve rows based on a condition m#axreStarlaieriveedCreolewbsritbieased on a condition s = smubasxeSta(clealeriberditCiees,lebrities = subset(celebrities, income==max(income)) income==max(income )) # print the result # print the result pprrini nt(tm(maxaSxaSlaarlieadriCeedleC e l e b ri t i e s ) brities)

Terminal Output

$ Rscript r_csv_analyze_data.R

name age income 7 Monica 29 45 $ Rscript r_csv_analyze_data.R

name age income 7 Monica 29 45

Write transformed data to CSV Files

Once we extract the required data or transform the data which is in data frame, we may write data frame to a CSV File.

Example R program to write an R data frame to CSV File

We shall use the above example, where we extracted rows with maximum income, and write the resulting rows to a CSV File.

r_csv_write_data.R - R Script File

# Example R program to write data to a CSV file

#set working directory - the directory containing csv file setwd("/home/arjun/

# Example R program to write data to a CSV file #set working directory - the directory containing csv file s e t w d ("/h o m e /a rj u n /w o rks p a ce /r") #read csv file celebrities = read.csv("sampleCSV.csv") # retrieve rows based on a condition maxSalariedCelebrities = subset(celebrities, income==max(income)) # write filtered data into a new CSV file. w ri t e .cs v(m a xSa l a ri e d C e l e b ri t i e s ,"re s u l t .cs v")

Terminal Output $ Rscript r_csv_write_data.R

$ Rscript r_csv_write_data.R

Conclusion :

In this R Tutorial ? R CSV Files, we have learnt to read CSV File using R, process data read from CSV using R and write process data to CSV using R.

Home - Get Started

R Tutorial R Script File R Working Directory R Data Types R Variables R Operators R Vectors R Matrix

Decision Making

R Decision Making

R if R if..else R if..else if...else R switch

Loops

R Loops R repeat loop R while loop R for loop R break

Strings

R Strings Find length of String in R Extract Substring from a String in R Concatenate two or more Strings in R

Functions

R Functions

DataFrame

R Data Frame Sort R Data Frame by Column For each row in an R Data Frame Import Excel Data into R Dataframe Convert R Dataframe to Matrix R Dataframe - Delete Rows R Dataframe - Drop Columns R Dataframe - Add Column R Dataframe - Change Column Name R Dataframe - Remove Duplicate Rows R Dataframe - Replace NA with 0 Convert Matrix to R Dataframe

Handling Data from Files

R CSV Files - Read, Filter, Write R Read Excel XLS XLSX files

Charts & Graphs

R Pie Charts R Line Graphs

Statistical Analysis

R Mean of a Vector R Median of a Vector

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download