source( "https://raw.githubusercontent.com/DS4PS/cpp-527-fall-2020/master/lectures/loop-example.R" ) head( d ) # data frame with X and Y get_sample_slope( d, n=10 ) # returns a single value test_for_null_slope( d, n=10 ) # returns a one-row data frame slopes <- NULL # collector vector for( i in 1:10 ) # iterator i { b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 } slopes slopes <- get_sample_slope( d, n=10 ) slopes for( i in 1:10 ) # iterator i { slopes <- NULL # collector vector b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 } slopes slopes <- NULL # collector vector for( i in c(3,7,10) ) # iterator i { b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 } slopes slopes <- NULL # collector vector x <- 5 for( i in 1:1000 ) # iterator i { b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 x <- i } x <- 5 for( i in 1:10 ) # iterator i { b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 x <- i } slopes slopes <- NULL # collector vector x <- 5 for( i in 1:10 ) # iterator i { b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 x <- i } slopes x x <- 1:10 x c( x, 43 ) x <- c( x, 43 ) x m <- lm( y ~ x, data=d ) summary( m ) class( m ) names( m ) m$coefficients m$residuals names( m ) models <- NULL # collector vector for( i in 1:10 ) # iterator i { m <- lm( y ~ x, data=d ) models[ i ] <- m } models models <- list() # collector vector for( i in 1:10 ) # iterator i { m <- lm( y ~ x, data=d ) models[[ i ]] <- m } models m models[[1]] names( models[[1]] ) feed_pig <- function( weight ) { weight + 1 } pig.weight <- 100 loop.count <- 1 while( pig.weight < 110 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) pig.weight <- new.weight print( pig.weight ) } old.weight <- pig.weight while( pig.weight < 110 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) pig.weight <- new.weight print( pig.weight ) } pig.weight <- 100 old.weight <- pig.weight while( pig.weight < 110 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) # pig.weight <- new.weight print( pig.weight ) } head( old.weight ) length( old.weight ) head( old.weight, 25 ) pig.weight <- 100 old.weight <- pig.weight while( pig.weight < 110 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) pig.weight <- new.weight print( pig.weight ) } old.weight pig.weight cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results cash <- 10 results <- NULL count <- 1 while( cash > 0 ) { cash <- cash + sample( c(-1,0,1), size=1 ) results[count] <- cash count <- count + 1 } results x <- c("MON","TUE","WED","THUR","FRI","SAT","SUN") vec <- sample( x, 100, replace=TRUE ) vec f <- factor( vec ) table( f ) table( f ) %>% prop.table() library( dplyr ) table( f ) %>% prop.table() levels( f ) vec unique( vec ) f2 <- f[ f %in% c("MON","TUE","WED","THUR","FRI") ] table( f2 ) levels( f2 ) f2 <- factor( f2 ) table( f2 ) unique( f2 ) unique( as.character(f2) ) x <- c("TUE","WED","FRI","SUN") vec <- sample( x, 20, replace=TRUE ) f3 <- factor( vec ) table( f3 ) vec all.days <- c("MON","TUE","WED","THUR","FRI","SAT","SUN") f3 <- factor( f3, levels=all.days ) table( f3 ) x <- c("TUE","WED","FRI","SUN") vec <- sample( x, 20, replace=TRUE ) unique( as.character( vec ) ) vec x <- sample( 1:3, size=20, replace=T ) x f <- factor( x, levels=c("blue","red","green") ) f x x <- as.character( x ) f <- factor( x, levels=c("blue","red","green") ) f args( factor )