Unit-3
Unit-3
Unit-3
Data Reshaping
Joining multiple vectors to form a data frame and combining two data frames
## df1
df1 <- data.frame(
id = 1:5,
name = c("Yawar", "Zayed", "Hanan", "Ziya", "Paras")
)
## df2
df2 <- data.frame(
id = c(2, 5, 4, 1, 3),
address = c("Saraibal", "Tral", "Hyderpora", "Khanyar", "Rajbagh")
)
Melting
library("reshape2")
library("reshape")
n <- c(1, 1, 2, 2)
time <- c(1, 2, 1, 2)
x <- c(6, 3, 2, 5)
y <- c(1, 4, 6, 9)
CSV Files
## Write into a csv file
anime <- data.frame(
Name = c("Naruto", "Bleach", "One Piece", "Dragon Ball"),
Year = c(2002, 2004, 1999, 1986),
Episodes = c(220, 366, 900, 153),
Main_Character = c("Naruto", "Ichigo", "Luffy", "Goku"),
villains = c("Orochimaru", "Aizen", "Blackbeard", "Freeza")
)
## Load library
library("xlsx")
Binary Files
## Get the column values (n = 25, because we have 20 values without cnames)
column_values <- readBin(read_filename, character(), n = 20)
final_data
class(final_data)
Xml Files
JSON Files
Databases
## Load the package
library("RMySQL")
## Create a db connection
mysql_con <- dbConnect(
MySQL(),
user = "yuhao",
password = "huoyuhao",
dbname = "laragigs",
host = "localhost",
local.infile = TRUE
)
R Charts
Pie chart
Bar chart
barplot(H,names.arg,main,xlab,ylab,col)
barplot(
values,
names.arg = labels,
xlab="Sports",
ylab="No. of students",
main = "Popularity of sports",
col = c("red", "green", "blue")
)
Box plot
x is a vector or a formula.
data is the data frame.
notch is a logical value. Set as TRUE to draw a notch.
varwidth is a logical value. Set as true to draw width of the box proportionate to the sample size.
names are the group labels which will be printed under each boxplot.
main is used to give a title to the graph
boxplot(
dataset$y ~ dataset$x^2,
data = dataset,
main = "Boxplot of x^2",
ylab = "x^2", xlab = "x",
names = c("1 sqr", "2 sqr", "3 sqr", "4 sqr", "5 sqr")
)
Histogram
hist(v,main,xlab,xlim,ylim,breaks,col,border)
Line Graphs
plot(v,main,xlab,ylab,col,type)
plot(
weights,
heights,
xlab="Weight",
ylab="Height",
main="Weight and Height of students",
ylim=c(170, 195),
xlim=c(50, 80)
)