HW2) Data Frames

pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

33B

Subject

Statistics

Date

Apr 3, 2024

Type

pdf

Pages

9

Uploaded by ProfProton22246

Report
qmd hw2- template.qmd hw2-first-last.qmd first last hw2- gaston-sanchez.qmd qmd embed-resources: true "tidyverse" starwars "tidyverse" "tidyverse" starwars # run this command in the console # do NOT include this command in your Qmd install.packages ( "tidyverse" )
"tidyverse" library(tidyverse) Rmd starwars "tibble" "data.frame" starwars starwars help(starwars) starwars # A tibble: 6 × 14 name height mass hair_color skin_color eye_color birth_year sex gender <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> 1 Luke Sky… 172 77 blond fair blue 19 male mascu… 2 C-3PO 167 75 <NA> gold yellow 112 none mascu… 3 R2-D2 96 32 <NA> white, bl… red 33 none mascu… 4 Darth Va… 202 136 none white yellow 41.9 male mascu… 5 Leia Org… 150 49 brown light brown 19 fema… femin… 6 Owen Lars 178 120 brown, gr… light blue 52 male mascu… # 5 more variables: homeworld <chr>, species <chr>, films <list>, # vehicles <list>, starships <list> starwars table() species Aleena Besalisk Cerean Chagrian Clawdite 1 1 1 1 1 Droid Dug Ewok Geonosian Gungan 6 1 1 1 3 Human Hutt Iktotchi Kaleesh Kaminoan 35 1 1 1 2 Kel Dor Mirialan Mon Calamari Muun Nautolan 1 2 1 1 1 Neimodian Pau'an Quermian Rodian Skakoan 1 1 1 1 1 Sullustan Tholothian Togruta Toong Toydarian # inspect first 5 rows of dataset head (starwars) starwars # your code table (starwars $ species)
1 1 1 1 1 Trandoshan Twi'lek Vulptereen Wookiee Xexto 1 2 1 2 1 Yoda's species Zabrak 1 2 nchar() [1] "Jabba Desilijic Tiure" "Wicket Systri Warrick" [1] "Rey" "BB8" | [1] 24 unique() unlist() [1] "X-wing" "Imperial shuttle" [3] "TIE Advanced x1" "Jedi starfighter" [5] "Trade Federation cruiser" "Naboo star skiff" [7] "Jedi Interceptor" "Belbullab-22 starfighter" [9] "Naboo fighter" "Millennium Falcon" # your code longestName <- max ( nchar (starwars $ name)) shortestName <- min ( nchar (starwars $ name)) charsWithLongestNames <- filter (starwars, nchar (name) == longestName) charsWithShortestNames <- filter (starwars, nchar (name) == shortestName) charsWithLongestNames $ name charsWithShortestNames $ name # your code nrow ( filter (starwars, eye_color == "blue" | eye_color == "red" )) # your code starships <- unlist (starwars $ starships) unique (starships)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
[11] "Slave 1" "A-wing" [13] "Naboo Royal Starship" "Scimitar" [15] "T-70 X-wing fighter" "H-type Nubian yacht" hist() sort() table() Naboo Tatooine Alderaan Coruscant Kamino 11 10 3 3 3 # your code hist (starwars $ height, col = "#adb0d9" , main = "Distribution of Heights" , xlab = "Height" ) # your codeta homeworlds <- sort ( table (starwars $ homeworld), decreasing = TRUE ) homeworlds[ 1 : 5 ]
# A tibble: 1 × 1 name <chr> 1 Jabba Desilijic Tiure lengths() [1] "Luke Skywalker" "C-3PO" "R2-D2" "Leia Organa" [5] "Obi-Wan Kenobi" "Chewbacca" "Yoda" "Palpatine" height films "A New Hope" "The Empire Strikes Back" "Return of the Jedi" mean_height [1] 172 # your code withBMI <- mutate (starwars, BMI = (mass) / ((height / 100 ) ** 2 )) maxBMI <- max (withBMI $ BMI, na.rm = TRUE ) select ( filter (withBMI, BMI == maxBMI), name) # your code stars <- filter (starwars, lengths (starwars $ films) > 4 ) stars $ name # your code filmFilt <- filter (starwars, films == "A New Hope" | films == "The Empire Strikes Back" | mean_height <- mean ( filter (filmFilt, species == "Human" ) $ height, na.rm = TRUE ) mean_height vecA = c ( "Luke Skywalker" , "Darth Vader" , "Leia Organa" , "Obi-Wan Kenobi" , "Chewbacca" ) vecB = c ( "lightsaber" , "cape" , "blaster" , "robe" , "crossbow" )
data.frame() vecA vecD gear "name" "equipment" "isJedi" "gearID" gear name equipment isJedi gearID 1 Luke Skywalker lightsaber TRUE 1 2 Darth Vader cape TRUE 2 3 Leia Organa blaster FALSE 3 4 Obi-Wan Kenobi robe TRUE 1 5 Chewbacca crossbow FALSE 4 starwars gear name starwars_gear1 all FALSE starwars_gear2 all.x TRUE starwars_gear1 starwars_gear2 name height mass hair_color skin_color eye_color birth_year 1 Chewbacca 228 112 brown unknown blue 200.0 2 Darth Vader 202 136 none white yellow 41.9 3 Leia Organa 150 49 brown light brown 19.0 4 Luke Skywalker 172 77 blond fair blue 19.0 5 Obi-Wan Kenobi 182 77 auburn, white fair blue-gray 57.0 sex gender homeworld species 1 male masculine Kashyyyk Wookiee 2 male masculine Tatooine Human 3 female feminine Alderaan Human 4 male masculine Tatooine Human 5 male masculine Stewjon Human films 1 The Empire Strikes Back, Revenge of the Sith, Return of the Jedi, A New Hope, The Force Awakens 2 The Empire Strikes Back, Revenge of the Sith, Return of the Jedi, A New Hope 3 The Empire Strikes Back, Revenge of the Sith, Return of the Jedi, A New Hope, The Force Awakens 4 The Empire Strikes Back, Revenge of the Sith, Return of the vecC = c ( TRUE , TRUE , FALSE , TRUE , FALSE ) vecD = c (1L, 2L, 3L, 1L, 4L) # your code gear <- data.frame ( "name" = vecA, "equipment" = vecB, "isJedi" = vecC, "gearID" = vecD) gear # your code starwars_gear1 <- merge (starwars, gear, by= "name" , all= FALSE ) starwars_gear2 <- merge (starwars, gear, by= "name" , all.x= TRUE ) head (starwars_gear1, 5 )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Jedi, A New Hope, The Force Awakens 5 The Empire Strikes Back, Attack of the Clones, The Phantom Menace, Revenge of the Sith, Return of the Jedi, A New Hope vehicles 1 AT-ST 2 3 Imperial Speeder Bike 4 Snowspeeder, Imperial Speeder Bike 5 Tribubble bongo starships 1 Millennium Falcon, Imperial shuttle 2 TIE Advanced x1 3 4 X-wing, Imperial shuttle 5 Jedi starfighter, Trade Federation cruiser, Naboo star skiff, Jedi Interceptor, Belbullab-22 starfighter equipment isJedi gearID 1 crossbow FALSE 4 2 cape TRUE 2 3 blaster FALSE 3 4 lightsaber TRUE 1 5 robe TRUE 1 name height mass hair_color skin_color eye_color birth_year 1 Ackbar 180 83 none brown mottle orange 41.0 2 Adi Gallia 184 50 none dark blue NA 3 Anakin Skywalker 188 84 blond fair blue 41.9 4 Arvel Crynyd NA NA brown fair brown NA 5 Ayla Secura 178 55 none blue hazel 48.0 sex gender homeworld species 1 male masculine Mon Cala Mon Calamari 2 female feminine Coruscant Tholothian 3 male masculine Tatooine Human 4 male masculine <NA> Human 5 female feminine Ryloth Twi'lek films 1 Return of the Jedi, The Force Awakens 2 The Phantom Menace, Revenge of the Sith 3 Attack of the Clones, The Phantom Menace, Revenge of the Sith 4 Return of the Jedi 5 Attack of the Clones, The Phantom Menace, Revenge of the Sith vehicles 1 2 head (starwars_gear2, 5 )
3 Zephyr-G swoop bike, XJ-6 airspeeder 4 5 starships equipment isJedi 1 <NA> NA 2 <NA> NA 3 Trade Federation cruiser, Jedi Interceptor, Naboo fighter <NA> NA 4 A-wing <NA> NA 5 <NA> NA gearID 1 NA 2 NA 3 NA 4 NA 5 NA df stats stats df$stats df["stats"] df(stats) df[stats] df[["stats"]] df[ ,"stats"] #read below tables
oski oski[-(1:5), ][seq(1, nrow(oski[-(1:5), ]), by=2), ] oski[-c(1:5, seq(1, nrow(oski), by=2)), ] oski[-c(1:5, seq(2, nrow(oski), by=2)), ] oski[(1:nrow(oski) > 5) & (1:nrow(oski) %% 2 != 0), ] oski[(1:nrow(oski) - 5) %% 2 == 1 & 1:nrow(oski) > 5, ] oski[6:nrow(oski) , ][6:nrow(oski) %% 2 == 1, ]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help