sum(seq(3,100,3))
## [1] 1683
length(seq(3,100,3))
## [1] 33
for (i in 101:200){
if(i%%3==0 & i%%4==0){
print(i)
}
}
## [1] 108
## [1] 120
## [1] 132
## [1] 144
## [1] 156
## [1] 168
## [1] 180
## [1] 192
for (i in 1:24){
if(24%%i==0){
print(i)
}
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 6
## [1] 8
## [1] 12
## [1] 24
mul <- 1
for (i in 1:10){
mul <- mul*i
}
mul
## [1] 3628800
month.name
## [1] "January" "February" "March" "April" "May" "June"
## [7] "July" "August" "September" "October" "November" "December"
The month of January
The month of February
The month of March
...(중략)
The month of October
The month of Novemer
The month of December
for(i in 1:12){
cat("The month of",month.name[i],'\n')
}
## The month of January
## The month of February
## The month of March
## The month of April
## The month of May
## The month of June
## The month of July
## The month of August
## The month of September
## The month of October
## The month of November
## The month of December
for(i in 1:9){
cat("9 x",i,"=",9*i,'\n')
}
## 9 x 1 = 9
## 9 x 2 = 18
## 9 x 3 = 27
## 9 x 4 = 36
## 9 x 5 = 45
## 9 x 6 = 54
## 9 x 7 = 63
## 9 x 8 = 72
## 9 x 9 = 81
i <- 1
while (i<10) {
cat("8 x",i,"=",8*i,'\n')
i <- i+1
}
## 8 x 1 = 8
## 8 x 2 = 16
## 8 x 3 = 24
## 8 x 4 = 32
## 8 x 5 = 40
## 8 x 6 = 48
## 8 x 7 = 56
## 8 x 8 = 64
## 8 x 9 = 72
for(i in 1:100){
if (i%%3==0){ # result <- ifelse(i%%3==0, '*', i)
cat("*"," ") # cat(result, ' ')
}else{cat(i," ")}
}
## 1 2 * 4 5 * 7 8 * 10 11 * 13 14 * 16 17 * 19 20 * 22 23 * 25 26 * 28 29 * 31 32 * 34 35 * 37 38 * 40 41 * 43 44 * 46 47 * 49 50 * 52 53 * 55 56 * 58 59 * 61 62 * 64 65 * 67 68 * 70 71 * 73 74 * 76 77 * 79 80 * 82 83 * 85 86 * 88 89 * 91 92 * 94 95 * 97 98 * 100
apply(iris[,1:4],1,sum)
## [1] 10.2 9.5 9.4 9.4 10.2 11.4 9.7 10.1 8.9 9.6 10.8 10.0 9.3 8.5 11.2
## [16] 12.0 11.0 10.3 11.5 10.7 10.7 10.7 9.4 10.6 10.3 9.8 10.4 10.4 10.2 9.7
## [31] 9.7 10.7 10.9 11.3 9.7 9.6 10.5 10.0 8.9 10.2 10.1 8.4 9.1 10.7 11.2
## [46] 9.5 10.7 9.4 10.7 9.9 16.3 15.6 16.4 13.1 15.4 14.3 15.9 11.6 15.4 13.2
## [61] 11.5 14.6 13.2 15.1 13.4 15.6 14.6 13.6 14.4 13.1 15.7 14.2 15.2 14.8 14.9
## [76] 15.4 15.8 16.4 14.9 12.8 12.8 12.6 13.6 15.4 14.4 15.5 16.0 14.3 14.0 13.3
## [91] 13.7 15.1 13.6 11.6 13.8 14.1 14.1 14.7 11.7 13.9 18.1 15.5 18.1 16.6 17.5
## [106] 19.3 13.6 18.3 16.8 19.4 16.8 16.3 17.4 15.2 16.1 17.2 16.8 20.4 19.5 14.7
## [121] 18.1 15.3 19.2 15.7 17.8 18.2 15.6 15.8 16.9 17.6 18.2 20.1 17.0 15.7 15.7
## [136] 19.1 17.7 16.8 15.6 17.5 17.8 17.4 15.5 18.2 18.2 17.2 15.7 16.7 17.3 15.8
apply(iris[,1:4],2,max)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 7.9 4.4 6.9 2.5
apply(mtcars,2,sum)
## mpg cyl disp hp drat wt qsec vs
## 642.900 198.000 7383.100 4694.000 115.090 102.952 571.160 14.000
## am gear carb
## 13.000 118.000 90.000
apply(mtcars,2,max)
## mpg cyl disp hp drat wt qsec vs am gear
## 33.900 8.000 472.000 335.000 4.930 5.424 22.900 1.000 1.000 5.000
## carb
## 8.000
apply(mtcars,2,sd)
## mpg cyl disp hp drat wt
## 6.0269481 1.7859216 123.9386938 68.5628685 0.5346787 0.9784574
## qsec vs am gear carb
## 1.7869432 0.5040161 0.4989909 0.7378041 1.6152000
> result <- lgm(10,8)
> result
[1] 2
> result <- lgm(10,20)
> result
[1] 10
lgm <- function(x,y){
for(i in 1:x){
if(x%%i==0 & y%%i==0){
max.common <- i
}
}
return(max.common)
}
result <- lgm(10,8)
result
## [1] 2
result <- lgm(10,20)
result
## [1] 10
> v1 <- c(7,1,2,8,9)
> result <- maxmin(v1)
> result$max ; result$min
[1] 9
[1] 1
> result <- maxmin(iris[,1])
> result$max ; result$min
[1] 7.9
[1] 4.3
maxmin <- function(v){
a <- max(v)
b <- min(v)
return(list(max=a,min=b))
}
v1 <- c(7,1,2,8,9)
result <- maxmin(v1)
result$max; result$min
## [1] 9
## [1] 1
result <- maxmin(iris[,1])
result$max; result$min
## [1] 7.9
## [1] 4.3
weight <- c(69, 50, 55, 71, 89, 64, 59, 70, 71, 80)
weight <- c(69, 50, 55, 71, 89, 64, 59, 70, 71, 80)
which.max(weight)
## [1] 5
which.min(weight)
## [1] 2
which(weight>=61 & weight<=69)
## [1] 1 6
weight.2 <- weight[which(weight<=60)]
weight.2
## [1] 50 55 59
iris[which.max(iris$Petal.Length),]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 119 7.7 2.6 6.9 2.3 virginica
iris[which(iris$Petal.Width>=0.3 & iris$Petal.Width<=0.4),]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 16 5.7 4.4 1.5 0.4 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 42 4.5 2.3 1.3 0.3 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 46 4.8 3.0 1.4 0.3 setosa
I saw her standing there; Misery; Anna (Go to him); Chains; Boys; Ask me why
remove <- function(st,pattern){
st <- gsub(pattern,"",st)
return(st)
}
remove("I saw her standing there; Misery; Anna (Go to him); Chains; Boys; Ask me why",";")
## [1] "I saw her standing there Misery Anna (Go to him) Chains Boys Ask me why"
Sepal.Length is numeric
Sepal.Width is numeric
Petal.Length is numeric
Petal.Width is numeric
Species is factor
It is neither a data frame nor a matrix.
cla <- function(data){
name <- colnames(data)
tryCatch(
for(i in 1:length(data)){
cat(name[i],"is",class(data[,i]),'\n')
}
, error=function(e) cat("It is neither a data frame nor a matrix.")
)
}
cla(iris)
## Sepal.Length is numeric
## Sepal.Width is numeric
## Petal.Length is numeric
## Petal.Width is numeric
## Species is factor
cla(LakeHuron)
## It is neither a data frame nor a matrix.
c(-5:5, Inf, -Inf, NA, NaN)
isEven <- function(x){
(x %% 2) == 0
}
isEven(c(-5:5, Inf, -Inf, NA, NaN))
## [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE NA
## [13] NA NA NA
c(-5:5, Inf, -Inf, NA, NaN)
count <- 0
count.Even <- function(x){
for(val in x){
if(!is.na(val%%2==0) & (val%%2==0))
count <- count+1
}
print(count)
}
count.Even(c(-5:5, Inf, -Inf, NA, NaN))
## [1] 5