# Title: Degbugging in R # Student name 1: # Student name 2: # Instructions: Please add comments for each bug you have found and fix it, such as: # bug 1, forget a comma. # Comments (e.g, known bugs, questions you would like Dr. Cao to go through in the class, etc): c1 <- "Hello World" c2 <- c(123,c1) c12 <- paste(c1 + c2,sep="") v3 <= c2[0] + 10 print(v3). print(substr(c12,2,40) x <- c(0/0, 1/0, 100, 200L) x(!as.na(x)) # get none NA values from x x <- list(c1, v3, x2) print(x(2) + 10) v1 <- seq(1,10,2) v2 <- seq(2,20,2) m<- matrix(cbind(v1,v2)) x <- data.frame(foo = 1:4, bar = c(T, T, F, F)) print(x$"foo"[1]) # get first value in foo column m1 <- matrix(1:6,2,3) m1[2,1] # want to print 4, but it is not 4 x <- c(1,3,5,7) y <- c(x+1) # value of y should be 2,4,6,8 m2 <- matrix(2:7,3,2) m12Mutiply <- m1 * m2 # I want to do the true matrix multiplication, but don't know why it's not working if(x[1]%2 == 0) { print("x[1] is even number") } data <- matrix(1:1000,100,10) for(i in data[[7]]) # I want to check data in 7 column, skip numbers less than 500, and exit the loop if a number is larger than 800 { if( i < 500 ) { next } }else( i > 800)} print(i) break # I want to check data in 7 column, skip numbers less than 500, and exit the loop if a number is larger than 800 for(i in data[[7]]) { print[i,7] if(data[[i,7]] >= 500 & data[[i,7]] <= 800) } cost <- c(50, 75, 90, 100, 150) food <- c("pizza", "burgers", "salads", "cheese", "pasta", "pasta") names(cost) <- food print(cost[0:3]) #I want to print the cost for first three food print(cost[2,3]) # I want to print cost for "Burgers" and "salads" print(cost.length) # I want to have the length of cost object n1 <- seq(1:100,2) # I want to Create a vector containing all the positive odd numbers smaller than 100. print(which.min(cost)) # print the minimum cost, learn which.min by yourself temp <- c(35, 88, 42, 84, 81, 30) city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto") city_temps <- dataFrame(name = city, temperature = temp) print(mean(temp2[!is.na(temp)])) # print the average temp for not NA temp <- (temp2 - 32)*5/9 # convert temperature into Celsius and overwrite the original values x <- seq(1:100) # create x with the numbers 1 through 100 sum(1/x^2) # Compute the sum 1 + 1/2^2 + 1/3^2 + ... + 1/100^2 x <- rnorm(1000, 0, 100) # generate 1000 random numbers low <- x<=0 which(low) # get all negative number sum(x0)) if(all(x>0)) { print("All Positives") } else{ print("Not All Positives") } sum_n <- function(n){ # create a function to calculate sum of int from 1 to n x <- 1:n print(sum(x)) } sum_n(10) log_plot <- function(x, y){ # this function will not return anything, it will make plot plot(log10(x), log10(y)) } log_plot(-1,100) # what happens for the following code: x <- 3 my_func <- function(y){ x <- 5 y print(x) } my_func(x) x # is x's value still 3? compute_s_n <- function(n){ x <- 1:n sum(x^2) } compute_s_n(-100) # I want to calculate sum of 1^2 + 2^2 +... + 100^2 # Create a vector for storing results s_n <- vector("numeric", 25) # Assign values to `n` and `s_n` for(i in 1:26){ s_n[i] <- compute_s_n(i) } #another idea for(i in 1:length(s_n)){ s_n[i] <- compute_s_n(i) } #another idea for(i in 1:seq_along(s_n)){ s_n[i] <- compute_s_n(i) } x<-0 system.time( for(i in 1:1e7) x <- x+i ) # "elapsed" gives us the total time needed to compute the command