Skip to content

Fill in missing data #854

@matthieugomez

Description

@matthieugomez

http://stackoverflow.com/questions/26171958/fill-in-missing-values-by-group-in-data-table

IMO, the best syntax would be a function fill_na

DT[, value_filled := fill_na(value, roll , rollends), by = id]

but it would require some work - I'm not even sure it's feasible.

An easier solution would be a function setna that works depending on the keys of DT

setna <- function(.data, cols,roll = TRUE ,  rollends = if (roll=="nearest") c(TRUE,TRUE)
  else if (roll>=0) c(FALSE,TRUE)
  else c(TRUE,FALSE)){
  keys <- key(.data)    
  if (length(keys)<1){
    stop(".data must be keyed by at least one variable")
  }   
  for (col in cols){
    eval(substitute(.data[, (col) := .data[!is.na(x)][.data, value, roll = roll, rollends = rollends]], list(x = as.name(col))))
  }
}
setkey(DT, id, date)
setna(DT, "value")

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions