Mr.Saravanan.S, Associate Professor (Computer Science), PAJANCOA & RI, Karaikal, U.T. of Puducherry,
Mr.Ayyoob.K.C., Assistant Professor (Statistics), KAU, Kerala
---
FOSS By Saravanan.S
Telegram Group Link
Ubuntu Linux Users
(Tested in Ubuntu 20.04)
Use Below Commands
To Install R (r-base)
sudo apt-get update
sudo apt-get install r-base
Ubuntu Linux Beginners May Skip This
(Tested in Ubuntu 20.04)
To Install Latest
r-base in Ubuntu 20.04
sudo gedit /etc/apt/sources.list
Add the below line
deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/
Save & Exit. Ignore Warning.
sudo apt-get update -o Acquire::AllowInsecureRepositories=true
sudo apt-get install r-base
(will also install r-base-core, r-doc-html, etc...)
(ignore warnings and press Y)
Instead of above, below command may be used for improved help features
sudo apt-get install r-base r-doc-info r-doc-pdf libjpeg62
For More Information:
Download RStudio Desktop
(As On 25.Oct.2020)
From Below URL
https://rstudio.com/products/rstudio/download/
Windows Users
Download .exe File
And Install It
Ubuntu 20.04 Users
(As On 25.Oct.2020)
Download .deb File
Given For Ubuntu 18
Run below command & install gdebi
sudo apt-get install gdebi
Right Click Over The .deb File >
Open With Other Application >
GDebi Package Installer >
Select > Install Package
RStudio Code Completions Completes
When Pressing Tab Or Enter Key
To Clear RStudio R Console
Press Ctrl + L
Example of Assignment Operator =
( <- can also be used instead of = )
numdata=6
numdata
[1] 6
numdata*10
[1] 60
To Find Absolute Value
numdata=-54
abs(numdata)
[1] 54
To Find The Square Root
numdata=64
sqrt(numdata)
[1] 8
To Find Sum
numvector=c(5,6,10)
sum(numvector)
[1] 21
To Find Mean (Average)
numvector=c(5,6,10)
mean(numvector)
[1] 7
To Find Median (Middle Value)
numvector=c(5,6,10)
median(numvector)
[1] 6
median(c(5,6,10,11))
[1] 8
median(c(5,11,9,6))
[1] 7.5
To Find Range
numvector=c(10,3,4,5,6,7,8)
range(numvector)
[1] 3 10
To Find Standard Deviation
numdata=c(4,5,6,7)
sd(numdata)
[1] 1.290994
To Find Variance (Square of Standard Deviation)
numdata=c(4,5,6,7)
var(numdata)
[1] 1.666667
To Create a dataframe (Table of Data) in R
Let dataframe Name Be marks
marks=data.frame()
fix(marks)
Subjects Student1 Student2 Student3
Maths 80 75 70
Physics 82 87 76
Chemistry 84 76 77
Biology 86 78 74
marks
Beginners May Skip This
For Large Datasets, Prefer data.table,
install.packages("data.table")
enhanced data.frame
To Import Dataset From Excel (sdata.xlsx) File
Click below link to download sdata.xlsx
https://drive.google.com/file/d/100LI1RYoZXINCwAK0UFQiecravJJZQzi/view?usp=sharing
Environment >
Import Dataset >
sdata.xlsx >
Import
sdata
Beginners May Skip This
When Running First Time,
Below Commands Used Automatically
install.packages("readxl")
library(readxl)
sdata <- read_excel("sdata.xlsx")
To List Available Datasets, From Loaded Packages
data()
Beginners May Skip This
To List Available Datasets, Including Datasets From Unloaded Packages
data(package=.packages(all.available = TRUE))
Example datasets Available By Default in R Installation
PlantGrowth
sleep
npk
Orange
Seatbelts
To Find Mean of weight Column of PlantGrowth dataset
mean(PlantGrowth$weight)
[1] 5.073
To Find Sum of weight Column of PlantGrowth dataset
sum(PlantGrowth$weight)
[1] 152.19
To Find Median (Middle Value) of weight Column of PlantGrowth dataset
median(PlantGrowth$weight)
[1] 5.155
To Find Range of age Column of Orange dataset
range(Orange$age)
[1] 118 1582
To Find Standard Deviation of age Column of Orange dataset
sd(Orange$age)
[1] 491.8645
To Find Variance (Square of Standard Deviation) of age Column of Orange dataset
var(Orange$age)
[1] 241930.7
To Find Covariance of age & circumference Columns of Orange dataset
cov(Orange$age, Orange$circumference)
[1] 25831.02
To Find Summary / Descriptive Statistics of PlantGrowth dataset
summary(PlantGrowth)
weight group
Min. :3.590 ctrl:10
1st Qu.:4.550 trt1:10
Median :5.155 trt2:10
Mean :5.073
3rd Qu.:5.530
Max. :6.310
To Find Simple Linear Regression (lm - Linear Model)
Syntax
summary(lm(<dependent variable>~<independent variable>, data=<data frame>))
Example
summary(lm(circumference~age, data=Orange))
Call:
lm(formula = circumference ~ age, data = Orange)
Residuals:
Min 1Q Median 3Q Max
-46.310 -14.946 -0.076 19.697 45.111
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.399650 8.622660 2.018 0.0518 .
age 0.106770 0.008277 12.900 1.93e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 23.74 on 33 degrees of freedom
Multiple R-squared: 0.8345, Adjusted R-squared: 0.8295
F-statistic: 166.4 on 1 and 33 DF, p-value: 1.931e-14
To Find Multiple Linear Regression (lm - Linear Model)
Syntax
summary(lm(<dependent variable>~<independent variable1 + independent variable2 + ... >, data=<data frame>))
Example
summary(lm(VanKilled~PetrolPrice + kms, data=Seatbelts))
Call:
lm(formula = VanKilled ~ PetrolPrice + kms, data = Seatbelts)
Residuals:
Min 1Q Median 3Q Max
-9.0120 -2.3903 0.3601 2.3563 6.5804
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.102e+01 2.003e+00 10.495 < 2e-16 ***
PetrolPrice -3.411e+01 2.025e+01 -1.684 0.0938 .
kms -5.622e-04 8.393e-05 -6.699 2.34e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.147 on 189 degrees of freedom
Multiple R-squared: 0.2592, Adjusted R-squared: 0.2513
F-statistic: 33.06 on 2 and 189 DF, p-value: 4.888e-13
Analysis of Variance (ANOVA) Without Any Interactions Between N, P & K of npk dataset
summary(aov(yield ~ block + N + P + K, data=npk))
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.3 68.66 4.288 0.01272 *
N 1 189.3 189.28 11.821 0.00366 **
P 1 8.4 8.40 0.525 0.47999
K 1 95.2 95.20 5.946 0.02767 *
Residuals 15 240.2 16.01
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Analysis of Variance (ANOVA) With Interactions Between N, P & K of npk dataset
summary(aov(yield ~ block + N * P * K, data=npk))
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.3 68.66 4.447 0.01594 *
N 1 189.3 189.28 12.259 0.00437 **
P 1 8.4 8.40 0.544 0.47490
K 1 95.2 95.20 6.166 0.02880 *
N:P 1 21.3 21.28 1.378 0.26317
N:K 1 33.1 33.13 2.146 0.16865
P:K 1 0.5 0.48 0.031 0.86275
Residuals 12 185.3 15.44
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Two Samples) Paired (Student's) t-test
t.test(extra ~ group, data = sleep, paired=TRUE)
Paired t-test
data: extra by group
t = -4.0621, df = 9, p-value = 0.002833
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.4598858 -0.7001142
sample estimates:
mean of the differences
-1.58
(Welch) (Two Samples) Unpaired (Independent) (Student's) t-test Assuming Unequal Variances
t.test(extra ~ group, data = sleep)
Welch Two Sample t-test
data: extra by group
t = -1.8608, df = 17.776, p-value = 0.07939
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.3654832 0.2054832
sample estimates:
mean in group 1 mean in group 2
0.75 2.33
(Two Samples) Unpaired (Independent) (Student's) t-test Assuming Equal Variances
t.test(extra ~ group, data = sleep, var.equal=TRUE)
Two Sample t-test
data: extra by group
t = -1.8608, df = 18, p-value = 0.07919
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.363874 0.203874
sample estimates:
mean in group 1 mean in group 2
0.75 2.33
Installing, Loading, Attaching agricolae Package for skewness function
RStudio >
Tools >
Install Packages... >
Search for agricolae >
Install
RStudio Packages Window(Pane) >
Search for agricolae >
Click To Load, Attach
(Needed For Fresh Session)
skewness(c(3,4,5,10))
[1] 1.597078
Beginners May Skip This
install.packages()
install.packages("agricolae")
library("agricolae")
RStudio Help Window
Search Box Available
Try With
aov
Orange
RStudio History Window
Double click on a line to copy to console
Scatter Plot Using Both Dots & Lines
plot(age~circumference, data=Orange, type="b")
Beginners May Skip This
Zoom
Export >
Save as Image /
Save as PDF /
Copy To Clipboard
Histogram
hist(PlantGrowth$weight)
Pie Chart
pie(table(Orange$age))
R Through Android in Offline
UserLAnd