Professional Documents
Culture Documents
Complied by
Dr. Vanita Joshi
2
How to install tidyverse and dplyr ?
Ex. The function accepts 2 or more parameters: the name of the data frame,
and the column(s) being selected.
select(mtcars, cyl, wt)
Ex. To select all data except the following columns: drat, vs, am, gear, and
carb
cars<- select(mtcars, -drat, -vs, -am, -gear, -carb)
print(cars)
Other Parameters
6
Select with parameters
filter() function used to filter data based on the row values, not the
columns.
Ex. To filter out all cars that have a gross weight over 4 tons, we would
use the filter() function as follows:
filter(mtcars, wt > 4)
Ex. To filter out all 8-cylinder cars that have more than four carburetors.
Separate each condition with a comma in the filter function. (one
can use & and | operator also for multiple conditions)
filter(mtcars, cyl == 8, carb > 4)
8
Arrange()
arrange() function used to sort data. The arrange function takes two or more
parameters: the name of the data frame, and the column(s) by which to sort.
Ex. To sort our table by cylinders and miles-per-gallon.
arrange(mtcars, cyl, mpg)
Use Starwars data set (from dplyr package) to answer the following:
1. Display all names starting with letter ‘L’
2. Display name, height & mass.
3. Display all fields except eye color, birth year and homeworld.
4. Take out all with hair color brown and mass more than 100.
5. Display all in descending order of height.
6. Display average height and mass.
7. Add a new variable for Height in feets.
8. Display median mass for all male and female.
9. Display total number of people with brown hair.
13