HW6_ Profiling and Scoping

pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

33B

Subject

Statistics

Date

Apr 3, 2024

Type

pdf

Pages

8

Uploaded by ProfProton22246

Report
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?