Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill in missing data #854

Closed
matthieugomez opened this issue Oct 3, 2014 · 2 comments · Fixed by #3341
Closed

Fill in missing data #854

matthieugomez opened this issue Oct 3, 2014 · 2 comments · Fixed by #3341
Assignees
Milestone

Comments

@matthieugomez
Copy link
Contributor

@matthieugomez matthieugomez commented Oct 3, 2014

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")
@matthieugomez matthieugomez changed the title Fil in missing data Fill in missing data Oct 3, 2014
@matthieugomez
Copy link
Contributor Author

@matthieugomez matthieugomez commented Oct 31, 2014

I've just seen that forderv worked on vectors. It's awesome. I was thinking, would you be able to do a version of match that rolls non matching elements? like match(a, l, roll, rollends). That would allow, among other things, the very nice syntax

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

@jangorecki jangorecki self-assigned this Jan 31, 2019
@jangorecki jangorecki added this to the 1.12.4 milestone Jan 31, 2019
jangorecki added a commit that referenced this issue Jan 31, 2019
jangorecki added a commit that referenced this issue Feb 2, 2019
jangorecki added a commit that referenced this issue Feb 3, 2019
@jangorecki
Copy link
Member

@jangorecki jangorecki commented Feb 3, 2019

When/if support for character columns will be provided, the following question can be answered: https://stackoverflow.com/q/20535505/2490497

mattdowle pushed a commit that referenced this issue Apr 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants