0% found this document useful (0 votes)
2 views5 pages

Practical 5

The document provides a practical guide on using R for data analysis, including importing datasets, aggregating data, and using the dplyr package for data manipulation. It demonstrates various functions such as reading data, summarizing, and filtering datasets like 'mtcars' and 'flights'. Additionally, it covers the use of the pipe operator for streamlined data operations.

Uploaded by

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

Practical 5

The document provides a practical guide on using R for data analysis, including importing datasets, aggregating data, and using the dplyr package for data manipulation. It demonstrates various functions such as reading data, summarizing, and filtering datasets like 'mtcars' and 'flights'. Additionally, it covers the use of the pipe operator for streamlined data operations.

Uploaded by

amisha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL 5 Amisha

22501104
install.packages("xlsx")
attach(book2)
book2
data=read.delim('clipboard')
data

Importing Datasets
> data()
> data(chickwts)
> head(chickwts)
weight feed
1 179 horsebean
2 160 horsebean
3 136 horsebean
4 227 horsebean
5 217 horsebean
6 168 horsebean
> nrow(chickwts)
[1] 71

Aggregate
> data("mtcars")
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> nrow(mtcars)
[1] 32
> ncol(mtcars)
[1] 11
> aggregate(disp~cyl, mtcars, mean)
cyl disp
1 4 105.1364
2 6 183.3143
3 8 353.1000
> tapply(mtcars$disp, mtcars$cyl, mean)
4 6 8
105.1364 183.3143 353.1000
> aggregate(disp~cyl+vs, mtcars, mean)
cyl vs disp
1 4 0 120.30
2 6 0 155.00
3 8 0 353.10
4 4 1 103.62
5 6 1 204.55
> aggregate(disp~cyl+vs+am, mtcars, mean)
cyl vs am disp
1 8 0 0 357.6167
2 4 1 0 135.8667
3 6 1 0 204.5500
4 4 0 1 120.3000
5 6 0 1 155.0000
6 8 0 1 326.0000
7 4 1 1 89.8000
dplyr
install.packages("dplyr")
library(dplyr)
data()
install.packages("nycflights13")
library(nycflights13)
View(flights)
> f5<-arrange(flights,year,dep_time)
> View(f5)
> f6<-arrange(flights, desc(dep_time))
> View(f6)

Grouping
> View(mtcars)
> by_gear <- mtcars %>% group_by(gear)
> View(by_gear)
> a<-summarise(by_gear, gear_1=sum(gear),gear2=mean(gear))
> a
# A tibble: 3 × 3
gear gear_1 gear2
<dbl> <dbl> <dbl>
1 3 45 3
2 4 48 4
3 5 25 5
> by_cyl <- mtcars %>% group_by(cyl)
> View(by_cyl)
> b <- summarise(by_cyl, cyll = sum(cyl), cy12 = mean(cyl))
> b
# A tibble: 3 × 3
cyl cyll cy12
<dbl> <dbl> <dbl>
1 4 44 4
2 6 42 6
3 8 112 8

Pipe Operator
> data(mtcars)
> View(mtcars)
> result <- mtcars %>% filter(mpg > 20) %>% sample_n(size = 10) %>% arrange(desc(mpg))
> result
mpg cyl disp hp drat wt qsec vs am gear carb
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
> #Select Operator
> mtcars_sel <- mtcars %>% select(mpg, hp, cyl)
> mtcars_sel
mpg cyl disp hp drat wt qsec vs am gear carb
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4

Select Operator
> #Select Operator
mtcars_sel <- mtcars %>% select(mpg, hp, cyl)
mtcars_sel
mpg hp cyl
Mazda RX4 21.0 110 6
Mazda RX4 Wag 21.0 110 6
Datsun 710 22.8 93 4
Hornet 4 Drive 21.4 110 6
Hornet Sportabout 18.7 175 8
Valiant 18.1 105 6
Duster 360 14.3 245 8
Merc 240D 24.4 62 4
Merc 230 22.8 95 4
Merc 280 19.2 123 6
Merc 280C 17.8 123 6
Merc 450SE 16.4 180 8
Merc 450SL 17.3 180 8
Merc 450SLC 15.2 180 8
Cadillac Fleetwood 10.4 205 8
Lincoln Continental 10.4 215 8
Chrysler Imperial 14.7 230 8
Fiat 128 32.4 66 4
Honda Civic 30.4 52 4
Toyota Corolla 33.9 65 4
Toyota Corona 21.5 97 4
Dodge Challenger 15.5 150 8
AMC Javelin 15.2 150 8
Camaro Z28 13.3 245 8
Pontiac Firebird 19.2 175 8
Fiat X1-9 27.3 66 4
Porsche 914-2 26.0 91 4
Lotus Europa 30.4 113 4
Ford Pantera L 15.8 264 8
Ferrari Dino 19.7 175 6
Maserati Bora 15.0 335 8
Volvo 142E 21.4 109 4

You might also like