Programming Application

중간고사


데이터 정보 Data info

  1. No : number of sation
  2. ST_NAME_E : station name in english
  3. ST_NAME_K : station name in korean
  4. ST_CODE : station code
  5. Control_og : organization controlling station
  6. BS_CODE : basic code
  7. Gage_type : measurement tool type
  8. River_system : river system of the station
  9. EL_m : elevation of station
  10. Time_Siries : availability of time siries data
  11. Daily_Siries : availability of time siries data
  12. Longitude : longitude with only degree
  13. Latitude : latitude with only degree
  14. Longitude_dms : longitude with degree, minute and second
  15. Latitude_dms : latitude with degree, minute and second
  16. X_EPSG5186 : x coordinate on TM coordinate system
  17. Y_EPSG5186 : y coordinate on TM coordiinate system

문제 Questions

  1. 강우과측소 데이터를 불러온다.
    Load rainfall staion data into R

  1. 전체 강우관측소 개수를 세어 n.st 변수에 입력한다.
    Count all rainfall station and define it as variable n.ts

  1. 각 통제 기관에 해당되는 관측소의 개수를 파악하여 n.st.eachOr에 입력한다. (plyr패키지의 count 함수 사용)
    Find out how many staions each of organization controlling station has control and define it as variable n.st.eachOr (use 'count' function in 'plyr' package)

  1. 경도 E128~129, 위도 N36~N38 범위에 해당되는 관측소의 개수를 n.st.range에 입력한다. (for 문과 if 문 사용)
    Determine how many staion is located in longitude E128~E129 and latitude N36~N38. And define it as variable n.st.range (use for loop, if statements)

  1. 두점 사이의 거리를 구하는 함수를 만들어라. (input: x1, y1, x2, y2. output: distance)
    Make function which can determine the distance between two points.

  1. 대기(Daegi)관측소와 등매(Deungmae)관측소의 거리를 계산하라. (TM좌표계인 X_EPSG5186과 Y_EPSG5186 좌표값은 m단위이다. 좌표값을 직접 입력하지 않고 관측소 이름으로 좌표값을 가져온다. grep 함수를 사용한다.)
    Determine the distance between Daegi station and Deungmae stattion (those rows X_EPSG5186 Y_EPSG5186 are made up by TM Coordinate System which is using meter distance. better not to use coordinates directly, but use station name to get coordinates. use 'grep' function)

  1. 다음과 같은 그래프를 그린다. (names 한수를 사용하여 벡터 데이터에 이름을 할당한다. barplot을 이용하여 그래프를 그린다. )
    Display plot like below (use functions 'barplot' and 'names' )

정답 R 소스 코드 Answers

# Packages ----------------------------------------------------------------
library("plyr")

# 1 Loading data -----------------------------------------------------------
rm(list = ls()) # Remove all variable
dat <- read.csv("./stPre.kr.csv")

# 2 counting number of sation ----------------------------------------------
n.st <- nrow(dat)

# 3 counting number of station each by organization -----------------------
n.st.eachOrg <- count(dat, vars = "Control_og")

# 4 counting number of station in the range ------------------------------
n.st.range <-0
for(i in 1:n.st){
	if(128<=dat$Longitude[i] && dat$Longitude[i] <=129 && 
		36<=dat$Latitude[i] && dat$Latitude[i] <= 38){
		n.st.range <- n.st.range +1}
}

# 5 Function calculating distance between two points ----------------------
f.dis <- function(x1, y1, x2, y2){
	out <- sqrt( (x1-x2)^2 + (y1-y2)^2 )
	return(out)
}

# 6 Estimating distance between two stations -------------------------------
st.1 <- "Daegi"
st.2 <- "Deungmae"
st.1.index <- grep(st.1, dat$ST_NAME_E)
st.2.index <- grep(st.2, dat$ST_NAME_E)

x1 <- dat$X_EPSG5186[st.1.index]
y1 <- dat$Y_EPSG5186[st.1.index]
x2 <- dat$X_EPSG5186[st.2.index]
y2 <- dat$Y_EPSG5186[st.2.index]

val.dis <- f.dis(x1 = x1, y1 = y1, x2 = x2, y2 = y2)

n.st.eachOrg.vec <- n.st.eachOrg[,2]
names(n.st.eachOrg.vec)<- n.st.eachOrg[,1]

barplot(n.st.eachOrg.vec, xlab = "Organization", ylab = "n", col=rgb(1,0.5,0.3), main ="Number of stations by organization" )