다음 함수는 작동하지만 마지막 as.Date 부분은 다소 이해가 안되는 시행 착오의 결과입니다. ifelse를 사용한 후 as.Date origin을 다시 설정해야하는 이유는 무엇입니까? 더 좋은 방법이 있습니까?
### This function creates a real date column out of year/period that is saved in
### in separate columns, plus it handles a 13th period in case of overlapping period
### terminology. Turns quarters into months.
realDate <- function (table,year="year_col",period="period_col"){
if (is.character(table) == TRUE)
{
dframe <- get(table)
}
else{
dframe <- table
}
x <- expression({resDate <- with(dframe,
as.Date(paste(get(year),"-",
ifelse(get(period) > 9, get(period),
paste("0", get(period), sep = "")),
"-01", sep = "")))
})
y <- expression({resDate <- with(dframe,as.Date(paste(get(year) + 1,"-","01","-01",sep="")))})
#### I do not get this? Why do I have to do this?
a <- ifelse(get(period) == 13,eval(y),eval(x))
a <-as.Date(a, origin="1970-01-01")
return(a)
}
대신 나는 (더 직관적으로 나에게했기 때문에) 이런 식으로 작업을 수행하려고 : 조건이 (NO) FALSE했다 때마다
{ ....
ifelse(get(period) == 13,eval(y),eval(x))
return(resDate)
}
이것은 수정 된 값을 반환하지만 경우 NA를 반환 조건은 TRUE (예)입니다. 왜 그런가요? 그리고 위의 함수를 사용하면 왜 다시 원점을 정의해야합니까? 왜 나 한테 전화 했어? 데이트?
편집 :
a <- rep(2002:2010,2)
b <- rep(1:13,2)
d<-cbind(a,b[1:length(a)])
names(d) <- c("year_col","period_col")
P.S : 나는 벡터화 ifelse에 thread을 발견했다.
몇 가지를 시도 좋을 것이다 입력에 대한 아이디어를 제공합니다. –
xx <-eval (x); yy <-eval (y); ifelse (get (마침표) == 13, yy, xx)'가 올바르게 작동합니까? – Marek
죄송합니다. 내 게시물을 편집하면 두 개의 열이있는 한 data.frame d에 모든 정보를 바인딩 할 수 있습니다. –