First Project

This is my first my R project. I am trying to upload the work i have done. iam using the inbuilt R dataset called “mt cars”

#1)importing the inbuilt dataset "mtcars"
data(mtcars)

#3)saving "mtcars" dataset as a .csv file in the working directory
write.csv(mtcars,"mtcarssaved.csv")

#2)creating a stucture for "mtcars"
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
sink("descrptive statistics.txt")
#4) mean of the variable "mpg" in the mtcars dataset
mean(mtcars$mpg)
## [1] 20.09062
#5)median of the variable "qsec" in mtcras datacars
median(mtcars$qsec)
## [1] 17.71
#6) descriptive statictics for "disp" variable
summary(mtcars$disp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    71.1   120.8   196.3   230.7   326.0   472.0
sink()

#7) save the output as "descriptive data"

#8) correlation between "mpg" and "wt"
cor(mtcars$mpg, mtcars$wt)
## [1] -0.8676594
#9) scatter plot between "disp" and "wt"
scatter.smooth(x=mtcars$disp, y=mtcars$wt, main="dispalcement vs. weight")

#10)regression between "mpg" and "wt"
linearModel <- lm(mpg ~ wt, data=mtcars)
linearModel
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Coefficients:
## (Intercept)           wt  
##      37.285       -5.344
#convert "cyl" into a factor(categorical variable)
mtcars<-factor(mtcars$cyl)


#checking whether converted or not 
str(mtcars)
##  Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...