HW2) Data Frames
pdf
keyboard_arrow_up
School
University of California, Berkeley *
*We aren’t endorsed by this school
Course
33B
Subject
Statistics
Date
Apr 3, 2024
Type
Pages
9
Uploaded by ProfProton22246
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
Related Documents
Related Questions
Write out the addition and multiplication tables for Z4.
arrow_forward
2x
OS dr
VI-T
arrow_forward
3.
7x
-3
arrow_forward
Write each spreadsheet expression in ordinary algebraic notation. Let cell A1 represent the variable x and A2 the variable y.
(a) +2*A1^2-7*A2 + 9
(b) +9* (A1^2 + 8)*(7*A1^2 - 7)^2
arrow_forward
Write each spreadsheet expression in ordinary algebraic notation. Let cell A1 represent the variable x and A2 the variable y.
(a)
+9*A1^2 – 4*A2 + 7
(b)
+9*(A1^2 + 8)*(5*A1^2 – 5)^2
arrow_forward
Aminah Abdulkhaalig-Batcha - X
+
m/presentation/d/12fcTT3Gik-q6baqBUOwKf008ZcbVJ2pToT3wBXGW1g4/edit#slide=id.g91af8d2b60_0_3
-Module 1 Quiz A
de Arrange Tools Add-ons Help
Last edit was seconds ago
O Present
D- Q - D Background
Layout-
Theme
Transition
Describe the domain, range, and end behavior.
11-
domain:
10
-9
8
-7
range:
6-
-5
4
end behavior:
3
-2
as x→00,
-1
as x→-00,
8 9 10 11 12 13
-1
-2-
You may copy and paste these symbols if you need them:
>
00
-00
10:13
Ở Settings
中
10/23/2
9 New Tab -
9 Aminah A.
Zoom
PgDn
End
PgUp
F10
PrtScn
F7
Home
FB
F9
F6
F5
8
5n
6
arrow_forward
Find the mode for the following variables 2,12,5,8,5,12,6,3,6,12,12
arrow_forward
In a certain year, there were 82 female officials in Congress, which is comprised of the House of Representatives and the Senate. If there were 50 more female
members of the House of Representatives than female senators, find the number of females in each house of Congress.
female senators and
female members of the House of Representatives.
There were
Save For Later
Submit Assignment
Check
2021 McGraw Hill LLC. All Rights Reserved. Terms of Use I Privacy Center Accessibility
MacBook Pro
G Search or type URL
esc
&
24
1
2
3
4
5
7
8
%3D
delete
Y
U
P
Q
W
tab
11
D
F
G
H
J
K
L
O 国 回
R
arrow_forward
3x2 • 14x - 24
End
Delete
Backspace
arrow_forward
please help with 1 , 2, 3, and 4
arrow_forward
Write out a multiplication table for S3
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you

Algebra: Structure And Method, Book 1
Algebra
ISBN:9780395977224
Author:Richard G. Brown, Mary P. Dolciani, Robert H. Sorgenfrey, William L. Cole
Publisher:McDougal Littell
Related Questions
- Write each spreadsheet expression in ordinary algebraic notation. Let cell A1 represent the variable x and A2 the variable y. (a) +2*A1^2-7*A2 + 9 (b) +9* (A1^2 + 8)*(7*A1^2 - 7)^2arrow_forwardWrite each spreadsheet expression in ordinary algebraic notation. Let cell A1 represent the variable x and A2 the variable y. (a) +9*A1^2 – 4*A2 + 7 (b) +9*(A1^2 + 8)*(5*A1^2 – 5)^2arrow_forwardAminah Abdulkhaalig-Batcha - X + m/presentation/d/12fcTT3Gik-q6baqBUOwKf008ZcbVJ2pToT3wBXGW1g4/edit#slide=id.g91af8d2b60_0_3 -Module 1 Quiz A de Arrange Tools Add-ons Help Last edit was seconds ago O Present D- Q - D Background Layout- Theme Transition Describe the domain, range, and end behavior. 11- domain: 10 -9 8 -7 range: 6- -5 4 end behavior: 3 -2 as x→00, -1 as x→-00, 8 9 10 11 12 13 -1 -2- You may copy and paste these symbols if you need them: > 00 -00 10:13 Ở Settings 中 10/23/2 9 New Tab - 9 Aminah A. Zoom PgDn End PgUp F10 PrtScn F7 Home FB F9 F6 F5 8 5n 6arrow_forward
- Find the mode for the following variables 2,12,5,8,5,12,6,3,6,12,12arrow_forwardIn a certain year, there were 82 female officials in Congress, which is comprised of the House of Representatives and the Senate. If there were 50 more female members of the House of Representatives than female senators, find the number of females in each house of Congress. female senators and female members of the House of Representatives. There were Save For Later Submit Assignment Check 2021 McGraw Hill LLC. All Rights Reserved. Terms of Use I Privacy Center Accessibility MacBook Pro G Search or type URL esc & 24 1 2 3 4 5 7 8 %3D delete Y U P Q W tab 11 D F G H J K L O 国 回 Rarrow_forward3x2 • 14x - 24 End Delete Backspacearrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Algebra: Structure And Method, Book 1AlgebraISBN:9780395977224Author:Richard G. Brown, Mary P. Dolciani, Robert H. Sorgenfrey, William L. ColePublisher:McDougal Littell

Algebra: Structure And Method, Book 1
Algebra
ISBN:9780395977224
Author:Richard G. Brown, Mary P. Dolciani, Robert H. Sorgenfrey, William L. Cole
Publisher:McDougal Littell