First Project

This is my first R Project. I am trying to upload the work I have done.I am using the inbuilt R dataset called ‘mtcars’.

#Importing the inbuilt dataset "mtcars"
data(mtcars)

#Saving "mtcars" dataset as a .csv file in our working directory

write.csv(mtcars, "mtcarssaved.csv")

sink("Descriptive Statistics.txt")
#Q.4.Mean of the variable 'mpg' in the mtcars dataset
mean(mtcars$mpg)
## [1] 20.09062
#Q.5.Median of the variable 'qsec' in 'mtcars' dataset
median(mtcars$qsec)
## [1] 17.71
#Q.6.Descriptive Statistics 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()

#Convert 'cyl' variable into a factor(categorical) variable

mtcars<-factor(mtcars$cyl)

str(mtcars)
##  Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
str(mtcars)
##  Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
data(mtcars)
#Q.7. Save the output as "Descriptive Statistics.txt"

getwd()
## [1] "/Users/sanjayfuloria"
setwd("/Users/sanjayfuloria/First Lesson on R")

getwd()
## [1] "/Users/sanjayfuloria/First Lesson on R"
#Q.8. Correlation between 'mpg' and 'wt'
cor(mtcars$mpg, mtcars$wt) 
## [1] -0.8676594
#Q.9. Scatter PLot between 'disp' and 'wt'

scatter.smooth(x=mtcars$disp, y=mtcars$wt, main="Displacement vs. Weight")

#Q.10. Regression between 'mpg' and 'wt'

linearModel <- lm(mpg ~ wt, data=mtcars)  # build linear regression model on full data
linearModel
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Coefficients:
## (Intercept)           wt  
##      37.285       -5.344