Q.
[ R ] 연습문제 (4)
yul_S2
2022. 11. 18. 12:28
반응형
1. 다음의 벡터 EMP는 ‘입사연도이름급여’순으로 사원의 정보가 기록된 데이터이다. 벡터 EMP를 이용하여 다음과 같은 출력 결과가 나타나도록 함수를 정의하시오.
EMP <- c(“2014홍길동220”, “2002이순신300”, “2010유관순260”
# 출력결과:
전체급여 평균: 260
평균 이상 급여 수령자
이순신 => 300
유관순 => 200
emp_pay<-function(x){ library(stringr) pay <- numeric() name <- character() indx<-1 for(n in x){ name[indx] <- str_extract(n, '[가-힣]{3}') #이름만 str<- str_extract(n, '[가-힣]{3}[0-9]{3}') #이름숫자 str<- str_replace(str,'[가-힣]{3}','') #이름제거 npay <- as.numeric(str) #숫자로변경 pay[indx] <- npay #급여저장 indx <- indx + 1 } avg <- mean(pay) cat("전체 급여 평균 :", avg,'\n') cat("평균 이상 급여 수령자\n") # 줄바꾸기 n <- 1: length(x) for(indx in n){ if(pay[indx]>=avg){ cat(name[indx], "=>", pay[indx],'\n') } } } emp_pay(EMP) |
2. 다음 조건에 맞게 client 데이터프레임을 생성하고, 데이터를 처리하시오
name <- c(“유관순”, “홍길동”, “이순신”, “신사임당”)
gender <- c(“F”, “M”, “M”, “F”) price <- c(50, 65, 45, 75)
1) 3개의 벡터 객체를 이용하여 client 데이터프레임을 생성하시오.
client <- data.frame(name, gender, price) client # name gender price # 1 유관순 F 50 # 2 홍길동 M 65 # 3 이순신 M 45 # 4 신사임당 F 75 |
2) price변수의 값이 65만원 이상이며 문자열 “beat”, 65만원 미만이면 문자열 “Normal” 을 변수 result에 추가하시오.
result <- ifelse(price >= 65, "Beat", "Normal") result # [1] "Normal" "Beat" "Normal" "Beat" |
3) result변수를 대상으로 빈도수를 구하시오.
table(result) # result # Beat Normal # 2 2 |
반응형