R_assignment#ch12


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

沒有留言:

張貼留言

季寧談管理讀後感

承蒙研究所恩師曾宗琳老師贈書,尤其在企管所唸了兩年之後,拜讀季寧的管理哲學,別有一番風味。公司管理究竟是可以用教科書教學而使管理人才有機會量產的一門學科,還是師父領進門,悟道與否自在徒弟有否慧根的一門藝術,自古便爭論不休。印象最深刻的,便是研究所參加台大 TMBA ...