This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
summary(mtcars)
mpg cyl disp
Min. :10.40 Min. :4.000 Min. : 71.1
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8
Median :19.20 Median :6.000 Median :196.3
Mean :20.09 Mean :6.188 Mean :230.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0
Max. :33.90 Max. :8.000 Max. :472.0
hp drat wt
Min. : 52.0 Min. :2.760 Min. :1.513
1st Qu.: 96.5 1st Qu.:3.080 1st Qu.:2.581
Median :123.0 Median :3.695 Median :3.325
Mean :146.7 Mean :3.597 Mean :3.217
3rd Qu.:180.0 3rd Qu.:3.920 3rd Qu.:3.610
Max. :335.0 Max. :4.930 Max. :5.424
qsec vs
Min. :14.50 Min. :0.0000
1st Qu.:16.89 1st Qu.:0.0000
Median :17.71 Median :0.0000
Mean :17.85 Mean :0.4375
3rd Qu.:18.90 3rd Qu.:1.0000
Max. :22.90 Max. :1.0000
am gear
Min. :0.0000 Min. :3.000
1st Qu.:0.0000 1st Qu.:3.000
Median :0.0000 Median :4.000
Mean :0.4062 Mean :3.688
3rd Qu.:1.0000 3rd Qu.:4.000
Max. :1.0000 Max. :5.000
carb
Min. :1.000
1st Qu.:2.000
Median :2.000
Mean :2.812
3rd Qu.:4.000
Max. :8.000
head(mtcars)
tail(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 ...
mtcars[1,]
mtcars[1:10,]
mtcars[3,4]
[1] 93
mtcars[1:2]
Car_data<-mtcars
View(Car_data)
dim(Car_data)
[1] 32 11
rownames(Car_data)
[1] "Mazda RX4" "Mazda RX4 Wag"
[3] "Datsun 710" "Hornet 4 Drive"
[5] "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D"
[9] "Merc 230" "Merc 280"
[11] "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC"
[15] "Cadillac Fleetwood" "Lincoln Continental"
[17] "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla"
[21] "Toyota Corona" "Dodge Challenger"
[23] "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9"
[27] "Porsche 914-2" "Lotus Europa"
[29] "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
colnames(Car_data)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt"
[7] "qsec" "vs" "am" "gear" "carb"
length(rownames(Car_data))
[1] 32
length(colnames(Car_data))
[1] 11
table(colnames(Car_data))
am carb cyl disp drat gear hp mpg qsec
1 1 1 1 1 1 1 1 1
vs wt
1 1
col_name<-c('mpg','hp','vs')
Car_data[col_name]
table(car_data$vs)
0
32
car_data[car_data$vs==0,]
table(rownames(car_data))
AMC Javelin Cadillac Fleetwood
1 1
Camaro Z28 Chrysler Imperial
1 1
Datsun 710 Dodge Challenger
1 1
Duster 360 Ferrari Dino
1 1
Fiat 128 Fiat X1-9
1 1
Ford Pantera L Honda Civic
1 1
Hornet 4 Drive Hornet Sportabout
1 1
Lincoln Continental Lotus Europa
1 1
Maserati Bora Mazda RX4
1 1
Mazda RX4 Wag Merc 230
1 1
Merc 240D Merc 280
1 1
Merc 280C Merc 450SE
1 1
Merc 450SL Merc 450SLC
1 1
Pontiac Firebird Porsche 914-2
1 1
Toyota Corolla Toyota Corona
1 1
Valiant Volvo 142E
1 1
car_data[car_data$hp<90,]
car_data[car_data$mpg>23 && hp>90,]
Get the unique count of cars
length(rownames(car_data))
[1] 32
Get mean of weight by cylinder
df_wt<-car_data[c('wt','cyl')]
attach(car_data)
The following objects are masked from car_data (pos = 3):
am, carb, cyl, disp, drat, gear, hp,
mpg, qsec, vs, wt
The following objects are masked from car_data (pos = 4):
am, carb, cyl, disp, drat, gear, hp,
mpg, qsec, vs, wt
The following objects are masked from car_data (pos = 5):
am, carb, cyl, disp, drat, gear, hp,
mpg, qsec, vs, wt
The following objects are masked from car_data (pos = 6):
am, carb, cyl, disp, drat, gear, hp,
mpg, qsec, vs, wt
split_df<-split(df_wt,cyl)
lapply(split(df_mpg$wt,df_wt$cyl),mean)
$`4`
[1] 2.285727
$`6`
[1] 3.117143
$`8`
[1] 3.999214
Get the no. of cars by gear
table(car_data$gear)
3 4 5
15 12 5
Get mean mpg by gear and carb and cylinder
library(dplyr)
Attaching package: 㤼㸱dplyr㤼㸲
The following objects are masked from 㤼㸱package:stats㤼㸲:
filter, lag
The following objects are masked from 㤼㸱package:base㤼㸲:
intersect, setdiff, setequal, union
car_data %>% group_by(gear,carb,cyl) %>%
summarise(mean_mpg=mean(mpg))
`summarise()` regrouping output by 'gear', 'carb' (override with `.groups` argument)
Which car has the max hp?
car_data[car_data$hp==max(car_data$hp),]
which car has the minimum displacement ?
car_data[car_data$disp==min(car_data$disp),]
Get summary of the data
summary(car_data)
mpg cyl disp
Min. :10.40 4:11 Min. : 71.1
1st Qu.:15.43 6: 7 1st Qu.:120.8
Median :19.20 8:14 Median :196.3
Mean :20.09 Mean :230.7
3rd Qu.:22.80 3rd Qu.:326.0
Max. :33.90 Max. :472.0
hp drat
Min. : 52.0 Min. :2.760
1st Qu.: 96.5 1st Qu.:3.080
Median :123.0 Median :3.695
Mean :146.7 Mean :3.597
3rd Qu.:180.0 3rd Qu.:3.920
Max. :335.0 Max. :4.930
wt qsec vs
Min. :1.513 Min. :14.50 0:32
1st Qu.:2.581 1st Qu.:16.89
Median :3.325 Median :17.71
Mean :3.217 Mean :17.85
3rd Qu.:3.610 3rd Qu.:18.90
Max. :5.424 Max. :22.90
am gear carb
Min. :0.0000 3:15 1: 7
1st Qu.:0.0000 4:12 2:10
Median :0.0000 5: 5 3: 3
Mean :0.4062 4:10
3rd Qu.:1.0000 6: 1
Max. :1.0000 8: 1
Out of all the cars with 4 cyl which car has max displ and min hp?
car_data[car_data]