2014-09-02 13 views
0

NA 값을 채우십시오. 즉, NA를 마지막으로 이전의 비 NA 값으로 바꾸십시오. 다음은 예제이지만 마지막 줄은 앞으로 채워지지 않습니다. 대체 할 값의 수가 대체 값의 수와 다른 오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?r에서 이전의 비 na 값을 지정하십시오.

"데이터 만 가지고 교체 9993 개 행이, 값 = C를 (2.5, 2.5, 4, 4 '를 cumTime" $<-.data.frame ( *tmp*, 오류가 "

# Test time accumulation and assignment 
foo_df <- NULL 
nTimes = 10000 
nEvents = 70 
nUnits = 300 
usageTimes = seq(0.5, 3, .5) 
events = c("File Event", paste("Event ",seq(1,nEvents))) 
randDates <- function(N, st="2014/01/01", et="2014/07/31") { 
    st <- as.POSIXct(as.Date(st)) 
    et <- as.POSIXct(as.Date(et)) 
    dt <- as.numeric(difftime(et,st,unit="sec")) 
    ev <- sort(runif(N, 0, dt)) 
    rt <- st + ev 
} 
probEvent = rep(1, length(events)) 
probEvent[1] = 5 
# Generate fake data with events, units, and event times 
foo_df = data.frame(eventName = sample(events, nTimes, replace=T, probEvent), 
    unit = sample(seq(1,nUnits),nTimes,replace=T), 
    event_time= randDates(nTimes), 
    usageTime = NA, cumTime=NA) 
# Order by time, and set the first nUnits events to File Event for each unit 
foo_df = foo_df[with(foo_df, order(event_time)), ] 
foo_df[1:nUnits ]$eventName = "File Event" 
foo_df[1:nUnits ]$unit = seq(1,nUnits) 
# Add random usage times to File Events 
nFile = length(foo_df$eventName[foo_df$eventName == "File Event"]) 
foo_df$usageTime[foo_df$eventName == "File Event"] = sample(usageTimes, nFile, replace=T) 
# Order by unit/event time 
foo_df = foo_df[with(foo_df, order(unit,event_time)), ] 

# accumulate the event time for file events 
entire_file_rows = foo_df$eventName=="File Event" 
temp_df = data.frame(cum_ft=0, event_time=foo_df$event_time[entire_file_rows], 
     unit=foo_df$unit[entire_file_rows], usageTime=foo_df$usageTime[entire_file_rows]) 
temp_df$cumTime <- ave(temp_df$usageTime, temp_df$unit, FUN=cumsum) 
foo_df$cumTime[entire_file_rows] = temp_df$cumTime 

# This is where I'm stuck 
# Want to assign the cummulative time to the other events (non File Event) 

library(zoo) ; 
# foo_df[foo_df$eventType != "File Event"]$"cumTime" <- NA 
foo_df$cumTime <- na.locf(foo_df$cumTime) 

는 I는 오류 메시지

두 가지 문제가 있음을 알 수 있습니다. 먼저 NA가 발생하기 때문에 na.locf에서 가져 오지 않고 두 번째로 locf는 단위로 묶어야합니다.

그러나 NA 먼저 데이터가 EventTime을 기준으로 정렬 된 다음 첫 번째 nUnit 레코드에 1부터 nUnit, eventName "File Event"라는 단위 번호가 할당되었습니다. ter, 단위 및 eventTime으로 정렬 할 때 "파일 이벤트"기록이 있기 전에 시간이 있습니까?

이 프로세스는 사용 시간을 cumTime 단위로 누적하고 레코드는 unit 순으로 정렬 한 다음 EventTime 순으로 정렬합니다. "파일 이벤트"에서 다른 이벤트로 cumTIme를 전송하기 전에, unitTime과 eventTime, eventTime으로 그룹화 된 cumTime을 플롯하고, 그 플롯은 괜찮아 보입니다. cumTime은 증가하고 있습니다. 그러나 "파일 이벤트"에서 다른 이벤트로 cumTime을 전송 한 후에는 누적 시간이 급증하고 점진적으로 감소하는 값이 있기 때문에 cumTime (단위/이벤트 유형별로 그룹화 됨)과 eventTime의 그래프가 올바르지 않습니다.

답변

4

값이 NA 인 문제가 있습니다. 이들은 na.locf에서 기본적으로 제거되어 할당의 오른쪽에 짧은 벡터가 표시됩니다.

당신은 최고의 NA 년대에 할당 할 수 있습니다

foo_df$cumTime <- na.locf(foo_df$cumTime, na.rm=FALSE) 

이것은 최고의 값을 제외한 모든 NA 값을 덮어 쓰게됩니다.

은 그럼 당신은 뭔가 다른 주요 NA 값을 지정할 수 있습니다 :

foo_df$cumTime[is.na(foo_df$cumTime)] <- 0 
+0

감사합니다. 간과 한 단위별로 그룹화하면 NA를 선도하는 경우 na.locf를 역으로 수행하는 것이 좋습니다. 단위별로 그룹화하려면 어떻게합니까? – user3969377

+0

또는 시도'na.fill (na.locf (... 뭐든간에 ...), 0)' –

+0

쏴, 나는 충분히 질문하지 않았다. 확장 된 질문을 읽고 답을 수정할 수 있습니까? "실제"데이터에서 cummulatated 시간은 이벤트 시간 경과와 함께 감소 할 수는 없지만이 오류는 단위/이벤트 시간별 정렬에 있다고 생각합니다. 이는 오류를 일으키는 동일한 오류 일 수 있습니다 na (이 경우 발생하지 않아야 함). – user3969377

0

오류가 첫 번째 nUnits 레코드 "파일 이벤트"와 장치를 할당하는 라인입니다. 올바른 라인은 첫 번째 라인은 사용 시간을 있지 않는 문제, 그리고 ($ cumTime foo_df) 명령 na.locf 레코드의 정확한 숫자가 생성되어 있지 않은 경우,

foo_df$eventName[1:nUnits ] = "File Event" 
foo_df$unit[1:nUnits ] = seq(1,nUnits) 

이다.

관련 문제