0% found this document useful (0 votes)
5 views14 pages

Final R-Codes

The document provides an overview of various R programming techniques, including data manipulation, statistical analysis, and regression modeling using datasets like 'mtcars' and custom data frames. It covers functions for calculating means, medians, and correlations, as well as creating scatter plots and performing linear regression. Additionally, it demonstrates how to create and combine data frames, access their elements, and calculate totals and percentages for student marks.

Uploaded by

Saksham Garg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Final R-Codes

The document provides an overview of various R programming techniques, including data manipulation, statistical analysis, and regression modeling using datasets like 'mtcars' and custom data frames. It covers functions for calculating means, medians, and correlations, as well as creating scatter plots and performing linear regression. Additionally, it demonstrates how to create and combine data frames, access their elements, and calculate totals and percentages for student marks.

Uploaded by

Saksham Garg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

R-codes

dim(Pivot_Table_Practice): dimensions (how many rows


and columns)
[1] 149 20

mydata<-Pivot_Table_Practice: rename my dataset

names(mydata): to find the names of columns

rownames(mydata) to find the names of rows

mydata$`Customer Name`

mean(mydata$Profit)
[1] 641.4257
> mean(mydata$Sales)
[1] 3175.091
> mean(mydata$`Shipping Cost`)
[1] 613.1659

median(mydata$Sales)
[1] 2761.2
> median(mydata$Profit)
[1] 527.04
> median(mydata$`Shipping Cost`)
[1] 585.25

sort(mydata$Profit) ascending order

sort(mydata$Sales,decreasing = FALSE): ascending


sort(mydata$Sales,decreasing = TRUE) :descending
summary(mydata$Sales):

Data Set 2

#data set in R
mtcars
?mtcars
help(mtcars)
dim(mtcars)
names(mtcars)
[Link](mtcars)
datanew<-mtcars
dim(datanew)
datanew$vs
datanew$hp
datanew$vs
sort(datanew$hp)
sort(datanew$wt)
sort(datanew$wt,decreasing = TRUE)
sort(datanew$wt,decreasing = FALSE)

mean(datanew$cyl)
median(datanew$cyl)
mode(datanew$cyl)
summary(datanew$cyl)
summary(datanew)
max(datanew$cyl)
head(datanew)
head(datanew)
tail(datanew)
cor(datanew$cyl,datanew$hp)
cor(datanew$mpg,datanew$cyl)
cor(datanew$mpg,datanew$hp)

#scatter plot
plot(datanew$mpg,datanew$hp)

plot(datanew$mpg, datanew$hp,xlab = "miles per


galon",ylab="horse power")

lines([Link](datanew$mpg,datanew$hp))

datasets:: use this code and it will give all


the options of dataset available in R studio.

25-3-2026

Simple linear Regression: One dependent variable


(DV), one independent variable (IDV). Example:
Advertising exp sales
IDV DV

Y=a+bx

Residuals: difference between Actual and predicted

Y=a+bx1+bX2+bx3
Y: DV
X: IDV
a: intercept
b: slope

Multiple linear Regression: One dependent variable (DV), more


than one independent variable (IDV’s)

Correlation and Scatter Plot


Mtcars (dataset)
> cor(mtcars$mpg,mtcars$cyl)
[1] -0.852162
> plot(mtcars$mpg,mtcars$cyl)
> plot(mtcars$mpg,mtcars$hp)
> plot(mtcars$mpg,mtcars$hp,xlab = "mileage per
gallon",ylab = "horse power")
> cor(mtcars$mpg,mtcars$hp)
[1] -0.7761684
> lines([Link](mtcars$mpg,mtcars$hp))

Regression: In R, put DV variable first, then IDV.

> lm(mtcars$mpg~mtcars$cyl)

Call:
lm(formula = mtcars$mpg ~ mtcars$cyl)

Coefficients:
(Intercept) mtcars$cyl
37.885 -2.876
> lm(mtcars$mpg~mtcars$hp)

Call:
lm(formula = mtcars$mpg ~ mtcars$hp)

Coefficients:
(Intercept) mtcars$hp
30.09886 -0.06823

> ml<-lm(formula = mtcars$mpg ~ mtcars$hp)


> ml

Call:
lm(formula = mtcars$mpg ~ mtcars$hp)

Coefficients:
(Intercept) mtcars$hp
30.09886 -0.06823

> summary(ml)

Call:
lm(formula = mtcars$mpg ~ mtcars$hp)

Residuals:
Min 1Q Median 3Q Max
-5.7121 -2.1122 -0.8854 1.5819 8.2360

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
mtcars$hp -0.06823 0.01012 -6.742 1.79e-07 ***
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.863 on 30 degrees of freedom


Multiple R-squared: 0.6024, Adjusted R-squared:
0.5892
F-statistic: 45.46 on 1 and 30 DF, p-value: 1.788e-07

> plot(ml)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
> par(mfrow=c(2,2))
> plot(ml)

Multiple Regression: In R, put DV variable first, then IDV’S.

> lm(mtcars$mpg~mtcars$cyl+mtcars$hp)

Call:
lm(formula = mtcars$mpg ~ mtcars$cyl + mtcars$hp)

Coefficients:
(Intercept) mtcars$cyl mtcars$hp
36.90833 -2.26469 -0.01912

> mml<-lm(formula = mtcars$mpg ~ mtcars$cyl +


mtcars$hp)
> mml

Call:
lm(formula = mtcars$mpg ~ mtcars$cyl + mtcars$hp)

Coefficients:
(Intercept) mtcars$cyl mtcars$hp
36.90833 -2.26469 -0.01912

> summary(mml)

Call:
lm(formula = mtcars$mpg ~ mtcars$cyl + mtcars$hp)

Residuals:
Min 1Q Median 3Q Max
-4.4948 -2.4901 -0.1828 1.9777 7.2934

Coefficients:
Estimate Std. Error t value
(Intercept) 36.90833 2.19080 16.847
mtcars$cyl -2.26469 0.57589 -3.933
mtcars$hp -0.01912 0.01500 -1.275
Pr(>|t|)
(Intercept) < 2e-16 ***
mtcars$cyl 0.00048 ***
mtcars$hp 0.21253
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1

Residual standard error: 3.173 on 29 degrees of freedom


Multiple R-squared: 0.7407, Adjusted R-squared:
0.7228
F-statistic: 41.42 on 2 and 29 DF, p-value: 3.162e-09
> plot(mml)
> par(mfrow=c(2,2))
> plot(mml)
> par(mfrow=c(1,1))
> plot(mml)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:

10-4-2026

> #vector
> city1<-c("delhi","mumbai")
> city1
[1] "delhi" "mumbai"
> View(city1)
> print(class(city1))
[1] "character"
> class(city1)
[1] "character"

> age2<-c(10,20,30)
> age2
[1] 10 20 30
> class(age2)
[1] "numeric"

#list
> city2<-list("delhi","mumbai","HP")
> city2
[[1]]
[1] "delhi"

[[2]]
[1] "mumbai"

[[3]]
[1] "HP"

> AGE3<-list(20,25,42)
> AGE3
[[1]]
[1] 20

[[2]]
[1] 25

[[3]]
[1] 42

> a<-list(20,c(20.1,23,25))
>a
[[1]]
[1] 20

[[2]]
[1] 20.1 23.0 25.0

> a1<-c(20.1,list(20.2,20.4,26))
> a1
[[1]]
[1] 20.1
[[2]]
[1] 20.2

[[3]]
[1] 20.4

[[4]]
[1] 26

> #Dataframe

First example:
>companyx=c(20000,30000,40000)
>companyy=c(30000,50000,60000)
>companyz=c(50000,80000,40000)
>datanew=[Link](companyx,companyy,companyz)
>datanew
>View(datanew)
Second example:
>data123=[Link](companyx=c(20000,30000,40000),companyy=c(30000,500
00,60000),companyz=c(50000,80000,40000))
>data123
Third example:

> companyA<-
[Link](days=c("monday","tuesday","wednesday"),sal
es=c(12000,15000,20000),profit=c(15000,25000,35000))
> print(companyA)
days sales profit
1 monday 12000 15000
2 tuesday 15000 25000
3 wednesday 20000 35000

> View(companyA)
> companyA

days sales profit


1 monday 12000 15000
2 tuesday 15000 25000
3 wednesday 20000 35000

Data summary

> mean(companyb$sales)
[1] 15666.67
> mean(companyA$profit)
[1] 25000
> summary(companyA$sales)
Min. 1st Qu. Median Mean 3rd Qu. Max.
12000 13500 15000 15667 17500 20000
> summary(companyA)
days sales profit
Length:3 Min. :12000 Min. :15000
Class :character 1st Qu.:13500 1st Qu.:20000
Mode :character Median :15000 Median :25000
Mean :15667 Mean :25000
3rd Qu.:17500 3rd Qu.:30000
Max. :20000 Max. :35000

> cor(companyA$sales,companyA$profit)
[1] 0.9897433
> plot(companyA$sales,companyA$profit)
> plot(companyA$sales,companyA$profit,xlab =
"sales",ylab = "profit")

>lm(companyA$profit~companyA$sales)

17-4-2026
#creation of dataset in r
x=[Link](Name=c("Amit","Radhika"),Age=c(22,24),gender=c("Male","Female"))
x
print(x)
View(x)
#ACCESSING THE ITEMS OF DATA FRAME
x$Age
x$gender
x[1]
x[2]
x[3]
#STRUCTURAL SIZE OF A DATA FRAME
dim(x)
size=dim(x)
size
ncol(x)
nrow(x)
#COMBINING DATA FRAMES
#Combining row wise
y=[Link](Name=c("Megha","Arun"),Age=c(25,27),gender=c("Female","Male"))
y
z=rbind(x,y)
z
#Combining coloumn wise

k=[Link](Salary=c(20000,30000),city=c("mumbai","delhi"))
k
xk=cbind(x,k)
xk
print(xk)
#Accessing the element of data frame

x[2,2]
x[3,2]
x[2,3]
#ADDING ROWS IN A DATA FRAME

x=[Link](Name=c("Amit","Radhika"),Age=c(22,24),gender=c("Male","Female"))
x
x1=rbind(x,c("Ram",28,"Male"))
x1
#ADDING COLOUMN IN A DATA FRAME

x2=cbind(x,city=c("East","West"))
x2
city=c("East","West")
city
x3=cbind(x,city)
x3
x4=c("Ram",28,"Male")
x4
x5=rbind(x,x4)
x5

17-4-2026

#1) Create data 2) Calculate mean and median 3) Calculate total and total percentage for each student 4)
create data of name, total, and percentage’.

#1
marks_1 <- [Link]( name = c("Anu","Dhruv","Vaibhav","Chirag"), maths = c(78,89,65,75), science =
c(77,88,55,99), english = c(98,95,96,94))
print(marks_1)

#2
mean(marks_1$maths)
mean(marks_1$science)
mean(marks_1$english)
median(marks_1$maths)
median(marks_1$science)
median(marks_1$english)
summary (marks_1$maths)
summary(marks_1)

#3
total <- (marks_1$maths+marks_1$science+marks_1$english)
print(total)
percentage <- (total/300)*100
print(percentage)

#4
name <- marks_1$name
output <- [Link](name,total,percentage)

You might also like