sampleData:
https://raw.githubusercontent.com/SangbuemCho/animalStatisticsDS/main/sampleDat.txt
library(tidyverse)
url <- "https://raw.githubusercontent.com/SangbuemCho/animalStatisticsDS/main/sampleDat.txt"
df <- read.table(url, header = T)
head(df, 10)
yearList <- which(is.na(df[, 1]) == FALSE)
X = df[, 1]
만약 불러온 데이터가 tibble 형태라면 아래와 같이 년도 컬럼 작성을 수정해야 함
X = df[, 1][[1]]
foo <- function(x){
a <- max(which((yearList <= x) == TRUE))
year = X[yearList[a]]
return(year)
}
lapply(1:nrow(df), foo) %>% unlist() -> m
df$year2 = m
head(df, 10)