Indexing data frame

library(SDSFoundations)

head(BikeData)
##   user_id age gender student employed               cyc_freq distance time
## 1       1  28      M       1        1                  Daily     3.25   15
## 2       2  35      M       0        1                  Daily     1.11    5
## 3       3  28      M       0        1                  Daily     5.59   23
## 4       4  44      F       0        1 Less than once a month     3.24   24
## 5       5  42      M       0        1 Several times per week     7.81   26
## 6       6  36      M       0        1 Several times per week     3.00   20
##   speed
## 1 13.00
## 2 13.32
## 3 14.58
## 4  8.10
## 5 18.02
## 6  9.00
names(BikeData)
## [1] "user_id"  "age"      "gender"   "student"  "employed" "cyc_freq"
## [7] "distance" "time"     "speed"
BikeData$age
##   [1] 28 35 28 44 42 36 45 54 39 44 29 32 40 29 26 39 33 32 43 29 51 36 29
##  [24] 30 30 35 27 37 27 25 25 39 51 61 26 29 33 29 41 23 40 31 38 46 47 32
##  [47] 36 36 44 52 25 36 27 44 51 24 48 26 44 36 42 32 26 40 32 45 31 41 42
##  [70] 34 34 26 43 31 36 48 63 37 33 27 29 41 38 30 29 22 61 25 40 34 36 38
##  [93] 29 36 29 33 29 37 43 30 40 28 25 44 42 33 37 32 51 54 30 34 34 43 33
## [116] 29 46 41 44 46 38
BikeData$cyc_freq
##   [1] Daily                   Daily                  
##   [3] Daily                   Less than once a month 
##   [5] Several times per week  Several times per week 
##   [7] Several times per week  Several times per week 
##   [9] Several times per week  Several times per month
##  [11] Daily                   Several times per week 
##  [13] Several times per week  Daily                  
##  [15] Daily                   Several times per week 
##  [17] Daily                   Daily                  
##  [19] Daily                   Several times per week 
##  [21] Several times per week  Several times per week 
##  [23] Several times per week  Several times per week 
##  [25] Several times per week  Daily                  
##  [27] Several times per week  Several times per week 
##  [29] Several times per week  Several times per week 
##  [31] Daily                   Daily                  
##  [33] Several times per month Several times per week 
##  [35] Several times per week  Several times per month
##  [37] Several times per week  Several times per month
##  [39] Daily                   Daily                  
##  [41] Several times per week  Several times per month
##  [43] Several times per week  Daily                  
##  [45] Several times per week  Several times per month
##  [47] Several times per month Daily                  
##  [49] Several times per week  Several times per week 
##  [51] Several times per week  Daily                  
##  [53] Daily                   Several times per month
##  [55] Daily                   Daily                  
##  [57] Less than once a month  Daily                  
##  [59] Several times per week  Daily                  
##  [61] Several times per week  Daily                  
##  [63] Several times per week  Several times per month
##  [65] Daily                   Several times per week 
##  [67] Several times per week  Several times per week 
##  [69] Several times per week  Daily                  
##  [71] Several times per week  Daily                  
##  [73] Several times per week  Several times per month
##  [75] Several times per week  Several times per week 
##  [77] Several times per week  Several times per week 
##  [79] Several times per week  Daily                  
##  [81] Daily                   Several times per week 
##  [83] Several times per month Several times per week 
##  [85] Daily                   Daily                  
##  [87] Several times per month Several times per week 
##  [89] Several times per week  Daily                  
##  [91] Daily                   Daily                  
##  [93] Several times per week  Several times per month
##  [95] Several times per week  Daily                  
##  [97] Several times per week  Daily                  
##  [99] Daily                   Daily                  
## [101] Several times per week  Daily                  
## [103] Daily                   Several times per week 
## [105] Daily                   Several times per week 
## [107] Daily                   Daily                  
## [109] Several times per week  Daily                  
## [111] Daily                   Daily                  
## [113] Several times per month Several times per week 
## [115] Several times per week  Several times per week 
## [117] Daily                   Several times per week 
## [119] Daily                   Several times per week 
## [121] Several times per week 
## 4 Levels: Daily Less than once a month ... Several times per week
daily_ride<- span=""> BikeData$cyc_freq=='Daily'
daily_ride
##   [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
##  [12] FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
##  [23] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE
##  [34] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [45] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
##  [56]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [67] FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [89] FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE
## [100]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
## [111]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
daily_ride<- span=""> BikeData$cyc_freq
daily_ride
##   [1] Daily                   Daily                  
##   [3] Daily                   Less than once a month 
##   [5] Several times per week  Several times per week 
##   [7] Several times per week  Several times per week 
##   [9] Several times per week  Several times per month
##  [11] Daily                   Several times per week 
##  [13] Several times per week  Daily                  
##  [15] Daily                   Several times per week 
##  [17] Daily                   Daily                  
##  [19] Daily                   Several times per week 
##  [21] Several times per week  Several times per week 
##  [23] Several times per week  Several times per week 
##  [25] Several times per week  Daily                  
##  [27] Several times per week  Several times per week 
##  [29] Several times per week  Several times per week 
##  [31] Daily                   Daily                  
##  [33] Several times per month Several times per week 
##  [35] Several times per week  Several times per month
##  [37] Several times per week  Several times per month
##  [39] Daily                   Daily                  
##  [41] Several times per week  Several times per month
##  [43] Several times per week  Daily                  
##  [45] Several times per week  Several times per month
##  [47] Several times per month Daily                  
##  [49] Several times per week  Several times per week 
##  [51] Several times per week  Daily                  
##  [53] Daily                   Several times per month
##  [55] Daily                   Daily                  
##  [57] Less than once a month  Daily                  
##  [59] Several times per week  Daily                  
##  [61] Several times per week  Daily                  
##  [63] Several times per week  Several times per month
##  [65] Daily                   Several times per week 
##  [67] Several times per week  Several times per week 
##  [69] Several times per week  Daily                  
##  [71] Several times per week  Daily                  
##  [73] Several times per week  Several times per month
##  [75] Several times per week  Several times per week 
##  [77] Several times per week  Several times per week 
##  [79] Several times per week  Daily                  
##  [81] Daily                   Several times per week 
##  [83] Several times per month Several times per week 
##  [85] Daily                   Daily                  
##  [87] Several times per month Several times per week 
##  [89] Several times per week  Daily                  
##  [91] Daily                   Daily                  
##  [93] Several times per week  Several times per month
##  [95] Several times per week  Daily                  
##  [97] Several times per week  Daily                  
##  [99] Daily                   Daily                  
## [101] Several times per week  Daily                  
## [103] Daily                   Several times per week 
## [105] Daily                   Several times per week 
## [107] Daily                   Daily                  
## [109] Several times per week  Daily                  
## [111] Daily                   Daily                  
## [113] Several times per month Several times per week 
## [115] Several times per week  Several times per week 
## [117] Daily                   Several times per week 
## [119] Daily                   Several times per week 
## [121] Several times per week 
## 4 Levels: Daily Less than once a month ... Several times per week
names(BikeData)
## [1] "user_id"  "age"      "gender"   "student"  "employed" "cyc_freq"
## [7] "distance" "time"     "speed"
BikeData$speed
##   [1] 13.00 13.32 14.58  8.10 18.02  9.00 15.33  9.55 16.74 10.09 13.08
##  [12] 11.16 13.07 10.48 19.31 18.42 13.36 14.01 15.04 16.10  9.71 14.83
##  [23] 15.32 15.44 13.02 11.69  8.70 15.81  8.43 15.19 12.50 10.74 13.54
##  [34] 20.93 16.68 14.16 19.89 16.03 15.19 25.76 18.22 13.36 11.50 16.80
##  [45] 17.17 15.65 30.30  6.79 13.60 12.28 12.74  9.45 11.87 15.48 16.03
##  [56] 13.51 14.80 10.39 13.20 13.50 12.33 17.38 14.64 11.01 18.90 15.86
##  [67] 11.44 12.69 15.58 13.32 13.67 18.62 14.17 10.70 20.89 17.95  9.84
##  [78] 15.00 13.16 12.55 17.51 14.46  7.69 14.86 13.86 12.75 11.05 12.56
##  [89] 13.03 14.63 13.85 15.72 10.40 16.29 15.14  9.70 14.08 15.65 12.96
## [100] 15.40 14.60 13.18 21.78 13.60 10.10 12.77 19.71 10.46 11.49  6.18
## [111] 16.59 15.20 11.10 30.84 11.87 13.80 12.21  4.29  9.80 13.57 13.19
View(BikeData)
table(BikeData$gender)
## 
##  F  M 
## 31 90
#Subset of Female by Indexing
BikeData$gender=='F'
##   [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [12] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [23] FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE
##  [34] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
##  [45] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [56] FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [67] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
##  [89] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
## [100] FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
## [111] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
females<- span=""> BikeData[BikeData$gender=='F',]
View(females)
class(BikeData)
## [1] "data.frame"
class(females)
## [1] "data.frame"
#Now successed making a female data.frame from BikeData data.frame and its called 'subsetting'
femalespeeds<- span=""> BikeData$speed[BikeData$gender=='F']
class(femalespeeds)
## [1] "numeric"
View(femalespeeds)
#Next successed making a vector that contains only female speed
mean(femalespeeds)
## [1] 13.65516
range(femalespeeds)
## [1]  4.29 30.30
summary(femalespeeds)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    4.29   10.24   13.57   13.66   15.76   30.30

Nikkei225

28000-28550 up in the early session, down lately.