p <- 1500
r <- 0.0324
t <- 0
for (n in c(1:72)){
p <- p * r /12 + p
}
print(p)
## [1] 1821.396
sprintf("%.2f",p)
## [1] "1821.40"
numv <- c(1:20)
numv
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
t <- 0
for(n in c(1:20)){
print(n)
if(n == 3 || (n%%3) == 0 ){
t <- t + n
}
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
## [1] 20
print(cat("-------------------",t))
## ------------------- 63NULL
sum(numv)
## [1] 210
sum(numv%%3)
## [1] 21
sum(numv[numv%%3 == 0])
## [1] 63
sum(numv[seq()])
## [1] 1
seq
## function (...)
## UseMethod("seq")
## <bytecode: 0x00000000070c9f50>
## <environment: namespace:base>
12 Use a for loop to calculate sum of i = 1 to 10 of x^i for the value x=2
i <- c(1:10)
sum <- 0
x = 2
for (n in 1:10){
sum <- sum + x ^ n
}
class(x)
## [1] "numeric"
sum
## [1] 2046
using while
i <- c(1:10)
sum <- 0
x = 2
i<- as.numeric(i)
c <- 1
while(c <= 10){
sum <- sum + x ^ c
c = c+1
}
print(sum)
## [1] 2046
Solve the problem from the previous two exercises without using a loop.
i <- c(1:10)
sum <- 0
x = 2
i<- as.numeric(i)
i
## [1] 1 2 3 4 5 6 7 8 9 10
gh <- sum(x^(1:10))
gh
## [1] 2046
12 factorial 12
total <- 1
for (n in 2:4){
total <- total*(n)
cat( "Factorial for " , n,"!",fill =TRUE)
print(c( n,total))
}
## Factorial for 2 !
## [1] 2 2
## Factorial for 3 !
## [1] 3 6
## Factorial for 4 !
## [1] 4 24
total <- 1
for (n in 2:4){
total <- total*(n)
#FIll Adding no new line
cat( paste0("Factorial for " , n,"!"," ="),fill =FALSE, total ,"\n")
#print(c( n,total))
}
## Factorial for 2! = 2
## Factorial for 3! = 6
## Factorial for 4! = 24
READ FILE The best way to read data from a CSV file is to use read.table. It might be tempting to use read.csv but that is more trouble than it is worth, and all it does is call read.table with some arguments preset. The result of using read.table is a data.frame. The first argument to read.table is the full path of the file to be loaded. The file can be sitting on disk or even on the Web. For the purposes of this book we will read from the Web. Any CSV will work but we have posted an incredibly simple CSV at http://www.jaredlander.com/data/Tomato%20First.csv. Let’s read that into R using read.table.
# Error as we are reading CSV
#theFile<- read.table("http://www.jaredlander.com/data/Tomato%20First.csv")
#NO Error
theFile<- read.csv("http://www.jaredlander.com/data/Tomato%20First.csv")
#using Read taBLE
#Changing this to other values such as "\t" (tab delimited) or ";" (semicolon delimited) allows it to read other types of files.
theFile<- read.table("http://www.jaredlander.com/data/Tomato%20First.csv",header = TRUE,sep =",")
View(theFile)
str(theFile)
## 'data.frame': 16 obs. of 11 variables:
## $ Round : int 1 1 1 1 2 2 2 2 3 3 ...
## $ Tomato : Factor w/ 16 levels "365 Whole Foods",..: 12 15 16 7 4 3 8 9 13 5 ...
## $ Price : num 3.99 2.99 0.99 3.99 5.49 4.99 3.99 3.99 4.53 NA ...
## $ Source : Factor w/ 10 levels "D Agostino","Eataly",..: 10 6 6 8 1 1 8 3 7 5 ...
## $ Sweet : num 2.8 3.3 2.8 2.6 3.3 3.2 2.6 2.1 3.4 2.6 ...
## $ Acid : num 2.8 2.8 2.6 2.8 3.1 2.9 2.8 2.7 3.3 2.9 ...
## $ Color : num 3.7 3.4 3.3 3 2.9 2.9 3.6 3.1 4.1 3.4 ...
## $ Texture : num 3.4 3 2.8 2.3 2.8 3.1 3.4 2.4 3.2 3.3 ...
## $ Overall : num 3.4 2.9 2.9 2.8 3.1 2.9 2.6 2.2 3.7 2.9 ...
## $ Avg.of.Totals: num 16.1 15.3 14.3 13.4 14.4 15.5 14.7 12.6 17.8 15.3 ...
## $ Total.of.Avg : num 16.1 15.3 14.3 13.4 15.2 15.1 14.9 12.5 17.7 15.2 ...
theFile<- read.table("http://www.jaredlander.com/data/Tomato%20First.csv",header = TRUE,sep =",",stringsAsFactors = FALSE)
str(theFile)
## 'data.frame': 16 obs. of 11 variables:
## $ Round : int 1 1 1 1 2 2 2 2 3 3 ...
## $ Tomato : chr "Simpson SM" "Tuttorosso (blue)" "Tuttorosso (green)" "La Fede SM DOP" ...
## $ Price : num 3.99 2.99 0.99 3.99 5.49 4.99 3.99 3.99 4.53 NA ...
## $ Source : chr "Whole Foods" "Pioneer" "Pioneer" "Shop Rite" ...
## $ Sweet : num 2.8 3.3 2.8 2.6 3.3 3.2 2.6 2.1 3.4 2.6 ...
## $ Acid : num 2.8 2.8 2.6 2.8 3.1 2.9 2.8 2.7 3.3 2.9 ...
## $ Color : num 3.7 3.4 3.3 3 2.9 2.9 3.6 3.1 4.1 3.4 ...
## $ Texture : num 3.4 3 2.8 2.3 2.8 3.1 3.4 2.4 3.2 3.3 ...
## $ Overall : num 3.4 2.9 2.9 2.8 3.1 2.9 2.6 2.2 3.7 2.9 ...
## $ Avg.of.Totals: num 16.1 15.3 14.3 13.4 14.4 15.5 14.7 12.6 17.8 15.3 ...
## $ Total.of.Avg : num 16.1 15.3 14.3 13.4 15.2 15.1 14.9 12.5 17.7 15.2 ...
dim(theFile)
## [1] 16 11