HW6_ Profiling and Scoping
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
8
Uploaded by ProfProton22246
qmd
hw6-
template.qmd
hw6-first-last.qmd
first
last
hw6-
gaston-sanchez.qmd
qmd
embed-resources: true
NA
NA
handle_na_1()
handle_na_1 <- function(data) {
for (col in names
(data)) {
if (
is.numeric
(data[[col]])) {
col_mean <- mean
(data[[col]], na.rm = TRUE
)
for (i in 1
:
length
(data[[col]])) {
if (
is.na
(data[[col]][i])) {
data[[col]][i] <- col_mean
}
}
}
else if (
is.integer
(data[[col]])) {
col_median <- as.integer
(
median
(data[[col]], na.rm = TRUE
))
for (i in 1
:
length
(data[[col]])) {
if (
is.na
(data[[col]][i])) {
data[[col]][i] <- col_median
}
}
}
else if (
is.character
(data[[col]])) {
for (i in 1
:
length
(data[[col]])) {
if (
is.na
(data[[col]][i])) {
data[[col]][i] <- "missing"
}
}
handle_na_()
}
}
return
(data)
}
handle_na_2()
handle_na_2 <- function(data) {
numeric_cols <- sapply
(data, is.numeric)
integer_cols <- sapply
(data, is.integer)
data <- data %>%
mutate
(
across
(
names
(data)[numeric_cols], ~
ifelse
(
is.na
(.), mean
(., na.rm = TRUE
), .))
mutate
(
across
(
names
(data)[integer_cols], ~
ifelse
(
is.na
(.), as.integer
(
median
(., na.rm
mutate
(
across
(
where
(is.character), ~
ifelse
(
is.na
(.), "missing"
, .)))
return
(data)
}
handle_na_3()
handle_na_3_modified <- function(data) {
data <- data %>%
mutate
(
across
(
where
(is.numeric), ~
ifelse
(
is.na
(.), mean
(., na.rm = TRUE
), .))) %>%
mutate
(
across
(
where
(is.integer), ~
ifelse
(
is.na
(.), as.integer
(
median
(., na.rm = TRUE
)
mutate
(
across
(
where
(is.character), ~
ifelse
(
is.na
(.), "missing"
, as.factor
(.))))
return
(data)
}
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
── Skip ('<text>:16:3'): Functions handle_na_1, handle_na_2 and handle_na_3 return the same output ──
Reason: handle_na_3 not found. Skipping test. Did you run the appropriate code chunk?
test_that
(
"Functions handle_na_1, handle_na_2 and handle_na_3 return the same output"
, {
skip_if_not
(
exists
(
"handle_na_1"
),
sprintf
(
"%s not found. Skipping test. Did you run the appropriate code chunk?"
, "handle_na_1"
)
)
skip_if_not
(
exists
(
"handle_na_2"
),
sprintf
(
"%s not found. Skipping test. Did you run the appropriate code chunk?"
, "handle_na_2"
)
)
skip_if_not
(
exists
(
"handle_na_3"
),
sprintf
(
"%s not found. Skipping test. Did you run the appropriate code chunk?"
, "handle_na_3"
)
)
set.seed
(
47
) test_df1 <- data.frame
(
numeric_col = c
(
NA
, runif
(
4
)),
integer_col = as.integer
(
c
(
sample
(
1
:
10
, 4
, replace = TRUE
), NA
)),
character_col = c
(
NA
, letters[
1
:
4
]),
stringsAsFactors = FALSE
)
result1 <- handle_na_1
(test_df1)
result2 <- handle_na_2
(test_df1)
result3 <- handle_na_3
(test_df1)
expect_equal
(result1, result2)
expect_equal
(result2, result3)
})
large_test_df
system.time()
Rprof()
user system elapsed 0.177 0.070 0.246 user system elapsed 0.013 0.000 0.014 Error in handle_na_3(large_test_df): could not find function "handle_na_3"
Timing stopped at: 0 0 0
handle_na_1: 0.24 handle_na_2: 0.005 Error in handle_na_3(large_test_df): could not find function "handle_na_3"
Timing stopped at: 0 0 0
num_rows <- 10000
scatter_na <- function(vec, na_prop = 0.5
) {
na_indices <- sample
(
1
:
length
(vec), size = floor
(na_prop * length
(vec)))
vec[na_indices] <- NA
return
(vec)
}
large_test_df <- data.frame
(
numeric_col = scatter_na
(
runif
(num_rows)),
integer_col = scatter_na
(
as.integer
(
sample
(
1
:
20
, num_rows, replace = TRUE
))),
character_col = c
(
sample
(
c
(letters, NA
), num_rows, replace = TRUE
)),
stringsAsFactors = FALSE
)
system.time
(result1 <- handle_na_1
(large_test_df))
system.time
(result2 <- handle_na_2
(large_test_df))
system.time
(result3 <- handle_na_3
(large_test_df))
cat
(
"handle_na_1:"
, system.time
(
handle_na_1
(large_test_df))[
"elapsed"
], "
\n
"
)
cat
(
"handle_na_2:"
, system.time
(
handle_na_2
(large_test_df))[
"elapsed"
], "
\n
"
)
cat
(
"handle_na_3:"
, system.time
(
handle_na_3
(large_test_df))[
"elapsed"
], "
\n
"
)
temperature <- 30
coolDown <- function() {
temperature
}
heatUp <- function() {
temperature <- 100
coolDown
()
}
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
[1] 30
[1] 7
heatUp
() # what does this return?
lightning <- function(intensity) {
intensity
}
storm <- function(lightning) {
lightning
(
7
)
}
storm
(
3
) # what does this return?
Related Documents
Related Questions
Give 2 characteristics that indicate a linear model may be appropriate to model a data set
arrow_forward
Create a scatter plot for the data: (2,3), (3,3), (5,1), (5,7), (8,2), (9,3)
Use the paperclip button below to attach files,
* Student can enter max 3500 characters
BIU
o search
F4
F5
F6
F7
F8
F9
F10
F2
必
ム+
2$
3
4
5
8.
W E R
T
60
23
arrow_forward
Is the independent variable of a function associated with the domain or range? Is the dependent variable associated with the domain or range?
arrow_forward
Use the scatterplot of Vehicle Registrations below to answer the questions
Vehicle Registrations in the United States, 1925-
2011 Vehicles millions
300
y = 3.0161x - 5819.5
R² = 0.9695
250
200
150
100
50
1920
1940
1960
1980
2000
2020
-50
Year
What is the dependent variable and what does it represent?
number of vehicle registrations
year
y =3.0161 x -5819.5
R^2 = 0.9695
not enough information to determine
Registrations (in millions)
arrow_forward
Perform the processing of dynamic rows, process the prediction
arrow_forward
Define the terms dependent and independent variable and explain their relationship.
arrow_forward
Determine if the set of ordered pairs represent a function:
B={ ( 1,4) , ( 2,5) , (-3,-4), ( -1,7), ( 0,4) }
arrow_forward
What linear function can be represented by the set of ordered pairs?
{(-4, 15), (0, 5), (4, -5), (8,-15)}
0-을+5
O -+5
O z+5
arrow_forward
is (2,4),(3,4),(4,4) a function
arrow_forward
Model the data witha linear function based on the data from 1995 and 1997. Let the inputvaraible be the number of years after 1995 and the output varaible represent the number of twin births in thousands. Thus two points would be (0, 96.7),(2, 104.1)
arrow_forward
Are they functions or no functions, answers needed
arrow_forward
How much should a healthy Shetland pony weigh? Let x be the age of the pony (in months), and let y be
the average weight of the pony (in kilograms). \table[[x, 3, 6, 12, 17, 26], [v, 60, 95, 140, 170, 175]] (a) Make a
scatter diagram of the data and visualize the line you think best fits the
arrow_forward
A line passes through the points
(4,23)
and
(8,27).
Write a linear function rule in terms of x and y for this line.
arrow_forward
A teacher is compiling data and needs to find the number of B grades. A B grade is any score greater than 79 and less than 90. If the data is listed in B1 through B210, which function would count the grades correctly?
Group of answer choices
=COUNTIFS(B1:B210,>79,B1:B210,<90)
=COUNTIF(B1:B210,">79")
=COUNTIFS(B1:B210,">79",B1:B210,"<90")
=COUNTIFS(B1:B210,">79","<90")
arrow_forward
What is the domain and range of the function
{(-3, 11), (–2, 2), (0, 2), (2, 6), (4, 18)}?
O Domain: {-3, -2,0, 2, 4}; Range: {11, 6, 2, 18}
O Domain: {11, 6, 2, 18} Range: {-3,-2,0, 2, 4}
O Domain: {-3, -2, 2, 4} Range: {11, 6, 2, 18}
O Domain: {11, 6, 2, 18} Range: {-3, –2, 2, 4}
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you

Glencoe Algebra 1, Student Edition, 9780079039897...
Algebra
ISBN:9780079039897
Author:Carter
Publisher:McGraw Hill

Elementary Linear Algebra (MindTap Course List)
Algebra
ISBN:9781305658004
Author:Ron Larson
Publisher:Cengage Learning
Related Questions
- Give 2 characteristics that indicate a linear model may be appropriate to model a data setarrow_forwardCreate a scatter plot for the data: (2,3), (3,3), (5,1), (5,7), (8,2), (9,3) Use the paperclip button below to attach files, * Student can enter max 3500 characters BIU o search F4 F5 F6 F7 F8 F9 F10 F2 必 ム+ 2$ 3 4 5 8. W E R T 60 23arrow_forwardIs the independent variable of a function associated with the domain or range? Is the dependent variable associated with the domain or range?arrow_forward
- Use the scatterplot of Vehicle Registrations below to answer the questions Vehicle Registrations in the United States, 1925- 2011 Vehicles millions 300 y = 3.0161x - 5819.5 R² = 0.9695 250 200 150 100 50 1920 1940 1960 1980 2000 2020 -50 Year What is the dependent variable and what does it represent? number of vehicle registrations year y =3.0161 x -5819.5 R^2 = 0.9695 not enough information to determine Registrations (in millions)arrow_forwardPerform the processing of dynamic rows, process the predictionarrow_forwardDefine the terms dependent and independent variable and explain their relationship.arrow_forward
- Determine if the set of ordered pairs represent a function: B={ ( 1,4) , ( 2,5) , (-3,-4), ( -1,7), ( 0,4) }arrow_forwardWhat linear function can be represented by the set of ordered pairs? {(-4, 15), (0, 5), (4, -5), (8,-15)} 0-을+5 O -+5 O z+5arrow_forwardis (2,4),(3,4),(4,4) a functionarrow_forward
- Model the data witha linear function based on the data from 1995 and 1997. Let the inputvaraible be the number of years after 1995 and the output varaible represent the number of twin births in thousands. Thus two points would be (0, 96.7),(2, 104.1)arrow_forwardAre they functions or no functions, answers neededarrow_forwardHow much should a healthy Shetland pony weigh? Let x be the age of the pony (in months), and let y be the average weight of the pony (in kilograms). \table[[x, 3, 6, 12, 17, 26], [v, 60, 95, 140, 170, 175]] (a) Make a scatter diagram of the data and visualize the line you think best fits thearrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Glencoe Algebra 1, Student Edition, 9780079039897...AlgebraISBN:9780079039897Author:CarterPublisher:McGraw HillElementary Linear Algebra (MindTap Course List)AlgebraISBN:9781305658004Author:Ron LarsonPublisher:Cengage Learning

Glencoe Algebra 1, Student Edition, 9780079039897...
Algebra
ISBN:9780079039897
Author:Carter
Publisher:McGraw Hill

Elementary Linear Algebra (MindTap Course List)
Algebra
ISBN:9781305658004
Author:Ron Larson
Publisher:Cengage Learning