results results[2] rm( results ) ls() results[ 1 ] <- "a" ls() results <- c("a","b","c") results results <- NULL results[ 1 ] <- "a" results[ 2 ] <- "b" results[ 3 ] <- "c" results 2^1 2^2 2^3 2^4 2^5 results <- NULL for( i in 1:5 ) { results[i] <- 2^i } results for( i in c("a","b","c") ) { print( i ) } results <- NULL results[ 1 ] <- "a" results[ 2 ] <- "b" results[ 3 ] <- "c" results length(results) results results["a"] results[1] results[c(1,3)] results[c(T,F,T)] results <- NULL for( i in 7:8 ) { results[i] <- 2^i } results 2&7 2^7 2^8 results <- NULL for( i in c(1,7:8) ) { results[i] <- 2^i } results results <- NULL results[1] <- 2^1 results class(results) length(results) results <- NULL for( i in c(1,7:8) ) { results[i] <- 2^i } results mean( results ) mean( results, na.rm=T ) sum(results,na.rm=T)/length(results) x <- c(1,7:8) 1:length(x) results <- NULL x <- c(1,7:8) for( i in 1:length(x) ) { results[i] <- 2^i } results x results <- NULL x <- c(1,7:8) for( i in 1:length(x) ) { results[i] <- 2^x } x[1] x[2] x[3] results <- NULL results[1] <- 2^x[1] results[2] <- 2^x[2] results[3] <- 2^x[3] results results <- NULL x <- c(1,7:8) for( i in 1:length(x) ) { results[i] <- 2^x[i] } results results <- NULL x <- c("a","b","c") for( i in 1:length(x) ) { results[ i ] <- x[1] } results LETTERS letters results <- NULL for( i in 1:500 ) { results[ i ] <- sample( LETTERS, size=1 ) } table( results ) results <- NULL for( i in 1:500 ) { results[ i ] <- sample( LETTERS[10], size=1 ) } table( results ) results <- NULL for( i in 1:500 ) { results[ i ] <- sample( LETTERS[1:10], size=1 ) } table( results ) results <- list() results results <- character(3) results results <- numeric(3) results results <- list(3) results ls() character(3) numeric(3) numeric(8) results <- list() for( i in 1:3 ) # I LOOP { for( j in 1:4 ) # J LOOP { car.position <- i first.pick <- j choice.combinations <- c( car.position, first.pick ) print( choice.combinations ) } } for( i in 1:3 ) # I LOOP { for( j in 1:4 ) # J LOOP { car.position <- i first.pick <- j choice.combinations <- c( car=car.position, pick=first.pick ) print( choice.combinations ) } } results <- list() loop.count <- 1 for( i in 1:3 ) # I LOOP { for( j in 1:4 ) # J LOOP { car.position <- i first.pick <- j choice.combinations <- c( car=car.position, pick=first.pick ) results[[ loop.count ]] <- choice.combinations loop.count <- loop.count + 1 } } results feed_pig <- function( weight ) { new.weight <- weight + 1 return( new.weight ) } current.pig.weight <- 100 current.pig.weight <- 100 # history of weights old.weight <- pig.weight loop.count <- 1 while( current.pig.weight < 200 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) current.pig.weight <- new.weight # to see data while loop is running # print( pig.weight ) } # history of weights old.weight <- pig.weight loop.count <- 1 while( current.pig.weight < 200 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) current.pig.weight <- new.weight # to see data while loop is running # print( pig.weight ) } pig.weight <- 100 # history of weights old.weight <- pig.weight loop.count <- 1 while( current.pig.weight < 200 ) { new.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, new.weight ) current.pig.weight <- new.weight # to see data while loop is running # print( pig.weight ) } current.pig.weight feed_pig <- function( weight ) { new.weight <- weight + 1 return( new.weight ) } pig.weight <- 100 feed_pig <- function( weight ) { new.weight <- weight + 1 return( new.weight ) } pig.weight <- 100 # history of weights old.weight <- pig.weight loop.count <- 1 while( pig.weight < 200 ) { pig.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, pig.weight ) # to see data while loop is running print( pig.weight ) } pig.weight <- 100 old.weight <- pig.weight # save history of weights while( pig.weight < 200 ) { pig.weight <- feed_pig( pig.weight ) old.weight <- c( old.weight, pig.weight ) print( pig.weight ) # preview current weight } # pig gains 1 lb each day feed_pig <- function( weight ) { new.weight <- weight + 1 return( new.weight ) } rnorm(1) args( rnorm ) feed_pig <- function( weight ) { new.weight <- weight + rnorm( n=1, mean=1, sd=1 ) return( new.weight ) } pig.weight <- 100 for( i in 1:5 ) { pig.weight <- feed.pig( pig.weight ) print( pig.weight ) } pig.weight <- 100 for( i in 1:5 ) { pig.weight <- feed_pig( pig.weight ) print( pig.weight ) } pig.weight <- 100 for( i in 1:100 ) { pig.weight <- feed_pig( pig.weight ) } pig.weight pig.weight <- 100 day.count <- 1 while( pig.weight < 200 ) { pig.weight <- feed_pig( pig.weight ) day.count <- day.count + 1 }