-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubset Observations.R
27 lines (17 loc) · 1.32 KB
/
Subset Observations.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
##################################Subset Observations#####################################################
#######################################################################################
#The provided R Tidyverse code snippets demonstrate how to create new data frames by filtering an existing data frame based on specific conditions.
#In the first snippet, a data frame named "males" is created by filtering the "class" data frame to include only rows where the value of the "Sex" variable is "M", indicating males. This is achieved using the filter function from the dplyr package.
#In the second snippet, a data frame named "preteen" is created by filtering the "class" data frame to include only rows where the value of the "Age" variable is either 11 or 12. This is done using the filter function and the %in% operator.
#Both code snippets showcase the power of the filter function in extracting specific subsets of data from a data frame based on given conditions. This allows for targeted analysis and further processing of the filtered data.
library(tidyverse)
class<-tribble(
~Name,~Sex,~Age,~Height,~Weight,
"Alfred","M",14,69,112.5,
"Alice","F",13,56.5,84,
"Carol","F",14,62.8,102.5,
"Henry","M",14,63.5,102.5,
"James","M",12,57.3,83,
)
males <- filter(class, Sex=="M")
preteen<-filter(class,Age %in% c(11,12))