R Markdown

1. Create a vector with 20 numbers. Include duplicates

numb_vect <- c(1:4, 4:10, 9:14, 1:3) numb_vect print(length(numb_vect))

2. Convert the vector above into a character vector

char_vect <- as.character(numb_vect) char_vect

3. Convert the first vector into a vector of factors

num_factr <- as.factor(numb_vect) num_factr

4. Show how many levels the vector of factors has (2 ways–levels of..

vectors are the same as the length of nique characters within the vectors)

nlevels(num_factr) length(unique(num_factr))

5. Create a vector that takes the first vector and ‘performs it’ on…

3*x**2 - 4x + 1 numb_vect <- 3 numb_vect^2 -4*numb_vect + 1 numb_vect

6. Create a named list–a list with several elements that are able to

… be referenced by name

named_list <- list(rank = 10, Iconic_HH_group = c(“Mos Def”, “Tablib Kwali”, “BlackStar”)) named_list$Iconic_HH_group[12]

7. Make a data frame with 4 columns, one each character, factor with 3 levels,

… numeric, and date. Should have at least 10 observations/rows.

#Cost of living in various US States

US_State <- as.character(c(“TX”, “NE”, “OH”, “KS”, “KY”, “OK”, “TN”, “NM”, “AZ”, “MO”)) Utilities_Cost <- c(“$120”, “$130”, “$140”, “$150”, “$160”, “$170”, “$180”, “$190”, “$200”, “$200”) Internet_Cost <- c(“$50”, “$55”, “$58”, “$52”, “$69”, “$57”, “$52”, “$54”, “$56”, “$88”) Gas_per_gal <- c(“$1.25”, “$1.35”, “$2.50”, “$5.50”, “$200”, “$3.25”, “$0.50”, “$0.77”, “$9.0”, "\(8000") df <- data.frame(US_State, Utilities_Cost, Internet_Cost, Gas_per_gal) df\)US_State <- as.character(df$US_State) str(df)

8.Illustrate how to add a row with a value for the factor column that isn’t already in the list of levels.

ADD: MI, $180, $60, $1.33,

dfnew <- data.frame(US_State = ‘MI’, Utilities_Cost = ‘$180’, Internet_Cost = ‘$60’, Gas_per_gal = ’\(1.33') str(dfnew) dfnew\)Gas_per_gal <- as.character(dfnew\(Gas_per_gal) dfnew\)US_State <- as.character(dfnew\(US_State) str(dfnew) dfnew\)US_State str(dfnew) df <- rbind(df, dfnew) df str(df)

9. Write code that implements the reading of a csv file, “temperatures”, from the current working directory

tmp <- read.csv(file = “temperatures.csv”) tmp

10. Use a loop to determine a final balance to the nearest cent.

Original balance is $1,500.

Account earns 3.24% interest compounded monthly after six years

i <- 1500 r <- 0.0324 cmpt <- 12 yrs <- 6

{ i <- i + (i * r / cmpt) } print(i) # prints 1821.396 sprintf(“%.2f”, i) #formats as 1821.40

i <- i * ( 1 + (r/cmpt)) ^ (cmpt * yrs) print (i)

11. Create a numeric vector of length 20. Create a program to sum every third element.

t <- 1:20 sum(t[seq(0, length(t), 3)])

12. Use a for loop to calculate the SUM of x^i, where i = (1,2,3,…,10)

x <- 2 k <- 0 for (i in 1:10) { k <- k + x^i

}

print (k)

13. User a for while loop to find the same problem

y <- 2 j <- 0 i <- 1 while (i <= 10) { j <- j + y^i i <- i+1

} print(j)

14. Solve the problem from the previous two exercises without using a loop

w <- 2 print(sum(x^(1:10)))