1. 不得使用R內建的函數,請設計下列函數
a.
最大值
mymax( )
mymax <-
function(x)
{
if(is.numeric(x) == FALSE)
print("This is not a vector")
else{
max_x <- x[1]
for(i in seq_along(x)){
if(x[i] >= max_x){
max_x <- x[i]
}else max_x <- max_x
}
print(max_x)
}
}
b.
最小值 mymin(
)
mymin <-
function(x)
{
if(is.numeric(x) == FALSE)
{
print("This is not a
vector")
}else {
min_x <- x[1]
for(i in seq_along(x)){
if(x[i] <= min_x){
min_x <- x[i]
}else min_x <- min_x
}
print(min_x)
}
}
c.
平均值 myave(
)
myave <-
function(x)
{
if(is.numeric(x) == FALSE){
print("This is not a
vector")
}else{
sum_x <- 0
for (i in seq_along(x)) {
sum_x <- sum_x + x[i]
}
ave_x <- sum_x /
length(x)
print(ave_x)
}
}
d.
排序 mysort(
)
mysort <-
function(x)
{
if(is.numeric(x) == FALSE){
print("This is not a
vector")
}else{
sorter <-
integer(length(x))
counter <- length(x)
repeat{
max_x<-max(x)
if (sum(x==max_x)>1){
sorter[counter:(counter-sum(x==max_x)+1)] <- max_x
}else{
sorter[counter] <-
max_x
}
counter <- counter -
sum(x==max_x)
if(counter <= 1)
break
x <-
x[-grep(max(x),x)]
}
return(sorter)
}
}
2. 電費收費函數
a.
每度100元
b.
超過300度打8折
c.
超過100杜但小魚等魚300度打9折
d.
政府再打7折、清寒再打5折
elec_fee <-
function(deg, customer, unitPrice = 100)
{
coefficent <- numeric(0)
for(i in deg){
if(i > 300) {index <-
1}
else if(i > 100 & i
<= 300){index <- 2}
else{index <- 3}
coefficent <- c(coefficent,
switch(index,0.8,0.9,1))
}
net.price <- deg *
unitPrice * coefficent
adj <- numeric(0)
for(j in customer){
adj <- c(adj, switch(j,
government = 0.7, poor = 0.5, 1))
}
finalPrice <- net.price *
adj
round(finalPrice)
}
3. 計算state.region 每區各有多少州
region_num <-
function(n)
{
Northeast <- 0
South <- 0
North.Central <- 0
West <- 0
for (i in n) {
if(i ==
"Northeast") Northeast
<- Northeast + 1
else if(i ==
"South") South <- South + 1
else if(i == "North
Central") North.Central <-
North.Central + 1
else West <- West + 1
}
region.count <-
c(Northeast, South, North.Central, West)
names(region.count) <-
c("Northeast", "South", "North.Central",
"West")
print(region.count)
}
4. 使用state.x77配合state.region 計算美國4大區
a.
人口數
state.x78 <-
cbind(state.x77,state.region)
state.pop <- state.x78[,c(1,9)]
head(state.pop)
region_pop <- c(sum(state.pop[state.pop[,2] ==
1,"Population"]),
sum(state.pop[state.pop[,2] == 2,"Population"]),
sum(state.pop[state.pop[,2]
== 3,"Population"]),
sum(state.pop[state.pop[,2] == 4,"Population"]))
names(region_pop) <- c("Northeast", "South",
"North.Central", "West")
region_pop
b.
面積
state.area <-
state.x78[,c(8,9)]
head(state.area)
region_area <- c(sum(state.area[state.area[,2] ==
1,"Area"]),
sum(state.area[state.area[,2] == 2,"Area"]),
sum(state.area[state.area[,2] == 3,"Area"]),
sum(state.area[state.area[,2] == 4,"Area"]))
names(region_area) <- c("Northeast", "South",
"North.Central", "West")
region_area
c.
平均收入
state.income <-
state.x78[,c(2,9)]
head(state.income)
region_aveincome <- c(mean(state.income[state.income[,2] ==
1,"Income"]),
mean(state.income[state.income[,2] == 2,"Income"]),
mean(state.income[state.income[,2] == 3,"Income"]),
mean(state.income[state.income[,2] == 4,"Income"]))
names(region_aveincome) <- c("Northeast",
"South", "North.Central", "West")
region_aveincome
沒有留言:
張貼留言