Reading Tabular Data from files in R Programming
read.table()
read.csv()
fromJSON()
read.xlxs()
Reading Data from Text File
read.table()
Parameters:
- file: Specifies the name of the file.
- header:The header is a logical flag indicating whether the first line is a header line contains data or not.
- nrows: Specifies number of rows in the dataset.
- skip: Helps in skipping of lines from the beginning.
- colClasses: It is a character vector which indicates class of each column of the data set.
- sep: It a string indicating the way the columns are separated that is by commas, spaces, colons, tabs etc.
read.table()
Example:
w3wiki.txt
read.table( "w3wiki.txt" ) |
Output:
Reading Data from a CSV File
read.csv()
read.csv()
read.table()
read.csv()
read.table()
read.csv()
Consider the table has 2000000 rows and 200 columns considering if all the columns are
of class numeric. 2000000 x 200 x 8 bytes/numeric #each number requires 8 bytes to be stored =3200000000/ bytes/MB =3051.76/MB =2.98 GB Approximately twice this amount of RAM i.e.5.96 GB will be required.
colClasses
nrows
Example:
w3wiki.csv
read.csv( "w3wiki.csv" ) |
Output :
Reading data from JSON File
fromJSON()
rjson
install.packages("rjson")
Example:
w3wiki.json
library(rjson) #loads the rjson library fromJSON( file = "w3wiki.json" ) |
Output:
as.data.frame(fromJSON( file = "w3wiki.json" )) |
Output:
Reading Excel Sheets
read.xlsx()
xlsx
install.packages("xlsx")
Example:
gfg.xlsx
library( "xlsx" ) #loads the xlsx library read.xlsx( "gfg.xlsx" , 1 ) #here 1 represents the sheet number |
Output:
read.xlsx()
read.xlsx2()
read.xlsx2()
read.xlsx(
read.xlsx2()
read.xlsx()
read.xlsx2()
readColumns()
Contact Us