#setup, include=FALSE
knitr::opts_chunk$set(echo = TRUE)
vec <- c(1,2,3,3,4,5,6,7,8,9,10,11,12,14,13,15,16,17,17,16)
str_vec <- as.character(vec)
str_vec
## [1] "1" "2" "3" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "14" "13"
## [16] "15" "16" "17" "17" "16"
fac_vec <- as.factor(vec)
fac_vec
## [1] 1 2 3 3 4 5 6 7 8 9 10 11 12 14 13 15 16 17 17 16
## Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
nlevels(fac_vec) #number of levels
## [1] 17
mod_vec <- 3 * vec ^ 2 - 4 * vec + 1
mod_vec
## [1] 0 5 16 16 33 56 85 120 161 208 261 320 385 533 456 616 705 800 800
## [20] 705
lst <- list('a','b','c')
names(lst) <- c('first','second','third')
lst
## $first
## [1] "a"
##
## $second
## [1] "b"
##
## $third
## [1] "c"
nmed_list <- list(test=1:5,counters = c('Joseph Chu','Andrea Bank'))
nmed_list$counters[1]
## [1] "Joseph Chu"
df_char <- c('a','b','c','d','e','f','g','h','i','j')
df_num <- c(1,2,3,4,5,6,7,8,9,10)
df_fac <- as.factor(c('apple','orange','kiwi','apple','orange','kiwi','apple','orange','kiwi','apple'))
df_date <- as.Date(c('2022-07-09','2022-07-10','2022-07-11','2022-07-09','2022-07-10','2022-07-11','2022-07-09','2022-07-10','2022-07-11','2022-07-14'))
df <- data.frame(df_char,df_num,df_fac,df_date)
names(df)<-c('char','num','fac','date')
new_frame <- data.frame(list('z',12,as.factor('melon'),as.Date('2022-07-18')))
names(new_frame)<-c('char','num','fac','date')
newdf <- rbind(df,new_frame)
newdf
## char num fac date
## 1 a 1 apple 2022-07-09
## 2 b 2 orange 2022-07-10
## 3 c 3 kiwi 2022-07-11
## 4 d 4 apple 2022-07-09
## 5 e 5 orange 2022-07-10
## 6 f 6 kiwi 2022-07-11
## 7 g 7 apple 2022-07-09
## 8 h 8 orange 2022-07-10
## 9 i 9 kiwi 2022-07-11
## 10 j 10 apple 2022-07-14
## 11 z 12 melon 2022-07-18
#temp_same_dir <- read.table(file='temperatures.csv',header=TRUE,sep=',')
#testing other dir
#wd <- getwd()
#temps = read.csv(paste(wd,'/Masters_Data_Sci/Bridge/temperatures.csv',sep=""),header = TRUE,sep=',')
#10.
tot_months <- 12 *6
int_rate <- 0.0324
bal <- 1200
for (i in 1:tot_months)
{
bal <- bal * 1.0324
print(bal)
}
## [1] 1238.88
## [1] 1279.02
## [1] 1320.46
## [1] 1363.243
## [1] 1407.412
## [1] 1453.012
## [1] 1500.09
## [1] 1548.693
## [1] 1598.87
## [1] 1650.674
## [1] 1704.155
## [1] 1759.37
## [1] 1816.374
## [1] 1875.224
## [1] 1935.981
## [1] 1998.707
## [1] 2063.465
## [1] 2130.322
## [1] 2199.344
## [1] 2270.603
## [1] 2344.17
## [1] 2420.121
## [1] 2498.533
## [1] 2579.486
## [1] 2663.061
## [1] 2749.344
## [1] 2838.423
## [1] 2930.388
## [1] 3025.333
## [1] 3123.353
## [1] 3224.55
## [1] 3329.025
## [1] 3436.886
## [1] 3548.241
## [1] 3663.204
## [1] 3781.892
## [1] 3904.425
## [1] 4030.928
## [1] 4161.531
## [1] 4296.364
## [1] 4435.566
## [1] 4579.279
## [1] 4727.647
## [1] 4880.823
## [1] 5038.962
## [1] 5202.224
## [1] 5370.776
## [1] 5544.789
## [1] 5724.44
## [1] 5909.912
## [1] 6101.394
## [1] 6299.079
## [1] 6503.169
## [1] 6713.871
## [1] 6931.401
## [1] 7155.978
## [1] 7387.832
## [1] 7627.198
## [1] 7874.319
## [1] 8129.447
## [1] 8392.841
## [1] 8664.769
## [1] 8945.508
## [1] 9235.342
## [1] 9534.567
## [1] 9843.487
## [1] 10162.42
## [1] 10491.68
## [1] 10831.61
## [1] 11182.55
## [1] 11544.87
## [1] 11918.92
#formatted balance
sprintf('%.2f',bal)
## [1] "11918.92"
eleven <- 1:20
eleven[eleven %% 3 == 0]
## [1] 3 6 9 12 15 18
second <- eleven[seq(3,length(eleven),3)]
second
## [1] 3 6 9 12 15 18
sum(second)
## [1] 63
num_vec = 1:20
sums <- list()
total <- 0
for (i in 1:10)
{
ele <- 2^i
total <- total + ele
sums[[length(sums)+1]] <- ele
}
print(total)
## [1] 2046
tot2<-0
x<-1
while(x<=10)
{
tot2 <-tot2+ 2^x
x <- x+1
}
print(tot2)
## [1] 2046
x2<-2
print(sum(x2^(1:10)))
## [1] 2046
#Second HW
num_test <- c(1:50,32:40,NA)
calc.mean <- function(nums)
{
if (is.numeric(nums)){
return(sum(num_test)/length(num_test))
}else{
return('Please input valid numeric vector')
}
}
calc.median <- function(nums)
{
num_sort <- sort(nums)
if (length(num_sort)%%2!=0) {
return(num_sort[length(num_sort)/2+1])
}else{
m1 <- num_sort[length(num_sort)/2]
m2 <- num_sort[length(num_sort)/2+1]
return(calc.mean(c(m1,m2)))
}
}
calc.mean(num_test)
## [1] NA
mean(num_test)
## [1] NA
calc.median(num_test)
## [1] 30
median(num_test)
## [1] NA
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.