지방이의 Data Science Lab

[R] 계절컬럼 파생변수 추가 본문

Data Analysis/깨R지식

[R] 계절컬럼 파생변수 추가

[지현] 2019. 12. 27. 02:45

library(stringr)

temp1$month = str_sub(temp1$yearmonth,5,6) 
temp1$month = as.numeric(temp1$month)
seasons = function(x){
  if(x %in% 2:4) return('Spring')
  if(x %in% 5:7) return('Summer')
  if(x %in% 8:10) return('Fall')
  if(x %in% c(11,12,1)) return('Winter')
}
temp1$season = sapply(temp1$month, seasons)

Comments