The collection of facts is known as data. Data can be in different forms. To analyze data using R programming Language, data should be first imported in R which can be in different formats like txt, CSV, or any other delimiter separated files. After importing data then manipulate, analyze, and report it.
Import Data from a File in R Programming Language
In this article, we are going to see how to import different files in R programming Language.
Import CSV file into R
Method 1: Using read.csv() methods.
Here we will import csv file using read.csv() method in R.
Syntax: read.csv(path, header = TRUE, sep = “,”)
Arguments :
- path : The path of the file to be imported
- header : By default : TRUE . Indicator of whether to import column headings.
- sep = “,” : The separator for the values in each row.
R
path <- "/gfg.csv"
content <- read.csv (path)
print (content)
|
Output:
ID Name Post Age
1 5 H CA 67
2 6 K SDE 39
3 7 Z Admin 28
Here we will use read.table() methods to import CSV file into R Programming Language.
Output:
Col1.Col2.Col3
1 100, a1, b1
2 200, a2, b2
3 300, a3, b3
Importing Data from a Text File
We can easily import or read .txt file using basic R function read.table(). read.table() is used to read a file in table format. This function is easy to use and flexible.
Syntax:
# read data stored in .txt file
x<-read.table(“file_name.txt”, header=TRUE/FALSE)
Output:
V1 V2 V3
1 100 a1 b1
2 200 a2 b2
3 300 a3 b3
If the header argument is set at TRUE, which reads the column names if they exist in the file.
Importing Data from a delimited file
R has a function read.delim() to read the delimited files into the list. The file is by default separated by a tab which is represented by sep=””, that separated can be a comma(, ), dollar symbol($), etc.
Syntax: read.delim(“file_name.txt”, sep=””, header=TRUE)
Output:
X.V1.V2.V3
1 1, 100, a1, b1
2 2, 200, a2, b2
3 3, 300, a3, b3
[1] "list
Here we are going to use rjson package to import the JSON file into R Programming Language.
R
library ( "rjson" )
res <- fromJSON (file = "E:\\exp.json" )
print (res)
|
Output:
$ID
[1] "1" "2" "3" "4" "5"
$Name
[1] "Mithuna" "Tanushree" "Parnasha" "Arjun" "Pankaj"
$Salary
[1] "722.5" "815.2" "1611" "2829" "843.25"
Importing XML file in R
To import XML file here we are going to use XML Package in R Programming language.
XML file for demonestration:
HTML
< RECORDS >
< STUDENT >
< ID >1</ ID >
< NAME >Alia</ NAME >
< MARKS >620</ MARKS >
< BRANCH >IT</ BRANCH >
</ STUDENT >
< STUDENT >
< ID >2</ ID >
< NAME >Brijesh</ NAME >
< MARKS >440</ MARKS >
< BRANCH >Commerce</ BRANCH >
</ STUDENT >
< STUDENT >
< ID >3</ ID >
< NAME >Yash</ NAME >
< MARKS >600</ MARKS >
< BRANCH >Humanities</ BRANCH >
</ STUDENT >
< STUDENT >
< ID >4</ ID >
< NAME >Mallika</ NAME >
< MARKS >660</ MARKS >
< BRANCH >IT</ BRANCH >
</ STUDENT >
< STUDENT >
< ID >5</ ID >
< NAME >Zayn</ NAME >
< MARKS >560</ MARKS >
< BRANCH >IT</ BRANCH >
</ STUDENT >
</ RECORDS >
|
Reading XML file:
It can be read after installing the package and then parsing it with xmlparse() function.
R
library ( "XML" )
library ( "methods" )
data <- xmlParse (file = "sample.xml" )
print (data)
|
Output:
1
Alia
620
IT
2
Brijesh
440
Commerce
3
Yash
600
Humanities
4
Mallika
660
IT
5
Zayn
560
IT
Importing SPSS sav File into R
Here we are going to read SPSS .sav File in R programming language. For this, we will use the haven package. To read SPSS files in R we use the read_sav() function which is inside the haven package.
Syntax: read_sav(“FileName.sav”)
R
library ( "haven" )
dataframe <- read_sav ( "SPSS.sav" )
dataframe
|
Output:
