Questions tagged [locf]

Last observation carried forward (LOCF), This method is specific to longitudinal data problems.

Last observation carried forward (LOCF) This method is specific to longitudinal data problems.

For each individual, missing values are replaced by the last observed value of that variable.

In scientific software for statistical computing and graphics, function na.locf from package zoo implements this method.

48 questions
18
votes
3 answers

How to replace NA (missing values) in a data frame with neighbouring values

862 2006-05-19 6.241603 5.774208 863 2006-05-20 NA NA 864 2006-05-21 NA NA 865 2006-05-22 6.383929 5.906426 866 2006-05-23 6.782068 6.268758 867 2006-05-24 6.534616 6.013767 868 2006-05-25 6.370312…
Arun
  • 447
  • 1
  • 5
  • 12
8
votes
2 answers

identify consecutively overlapping segments in R

I need to aggregate overlapping segments into a single segment ranging all connected segments. Note that a simple foverlaps cannot detect connections between non overlapping but connected segments, see the example for clarification. If it would…
rluech
  • 606
  • 4
  • 15
5
votes
2 answers

Limit na.locf in zoo package

I would like to do a last observation carried forward for a variable, but only up to 2 observations. That is, for gaps of data of 3 or more NA, I would only carry the last observation forward for the next 2 observations and leave the rest as NA. If…
user3507584
  • 3,246
  • 5
  • 42
  • 66
3
votes
3 answers

R last observation carried forwards and backwards up to n rows

Is there a good way to carry the last observation of a row both forward and backwards n times? example vector, to demonstrate: Before change: vector <- c(NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA, NA, 3, NA, NA, NA, NA) After…
3
votes
2 answers

Fill empty cells between two values in column with last non empty cell and next non empty cell in R

I need to loop over IDs in a dataframe to fill NA values in a column by attributing empty cells evenly between the last and first filled entry outside of the NA cells. ID Value X Y 1 A x y 1 NA x y 1 …
shinama99
  • 51
  • 6
3
votes
2 answers

equivalent of na.locf in sparkR

I am new to R trying to rewrite an R code in sparkR. One of the operations on data.table named costTbl (which has 5 other columns) is costTbl[,cost:=na.locf(cost,na.rm=FALSE),by=product_id] costTbl[,cost:=na.locf(cost,na.rm=FALSE,…
raizsh
  • 456
  • 1
  • 6
  • 16
3
votes
1 answer

Last observation carried forward by group over multiple columns

I have a dataset with observations of multiple patients and their diagnoses over time. There are 9 different dummy variables, each representing a specific diagnosis, named e.g. L40, L41, K50, M05 and so on. Where there are missing values in the…
udden2903
  • 783
  • 6
  • 15
3
votes
2 answers

How to do a last observation carrying forward using SAS PROC SQL

I have the data below. I want to write a sas proc sql code to get the last non-missing values for each patient(ptno). data sda; input ptno visit weight; format ptno z3. ; cards; 1 1 122 1 2 123 1 3 . 1 4 . 2 1 156 2 2 . 2 3 70 2 4 . 3 1 60 3 2 . 3…
johnww
  • 65
  • 2
  • 9
3
votes
2 answers

Is Last Observation Carried Forward (LOCF) implemented in PostgreSQL?

Is the data imputation method Last Observation Carried Forward (LOCF) implemented in PostgreSQL? If not, how could I implement this method?
Hello lad
  • 17,344
  • 46
  • 127
  • 200
2
votes
1 answer

How to set a max range condition with timescale time_bucket_gapfill() in order to not fill real missing values?

I'd like some advices to know if what I need to do is achievable with timescale functions. I've just found out I can use time_bucket_gapfill() to complete missing data, which is amazing! I need data each 5 minutes but I can receive 10 minutes, 30…
CamSim
  • 21
  • 2
2
votes
1 answer

Change maxgap for number of times a value is carried forward

I have a data frame similar to the following: library(data.table) test <- data.table(data.frame("value" = c(5,NA,8,NA,NA,8,6,NA,NA,10), "locf_N" = c(1,NA,1,NA,NA,1,2,NA,NA,2)) ) In this data frame I have a variable…
user3507584
  • 3,246
  • 5
  • 42
  • 66
2
votes
2 answers

Bridge the last and next non-NA value with intermediate values that grow evenly

What would be a good way to fill the missing NAs in a dataframe column with intermediate values that grow gradually from the last non-NA value to the next non-NA value? Here is an example: for the column cost, I would like to obtain the column…
Julien Massardier
  • 1,326
  • 1
  • 11
  • 29
2
votes
2 answers

Next "specific" observation carried backward (NOCB)

This is my data frame: library(zoo) library(dplyr) df <- data.frame( id = rep(1:4, each = 4), status = c( NA, "a", "c", "a", NA, "c", "c", "c", NA, NA, "a", "c", NA, NA, "c", "c"), otherVar = letters[1:16], …
Roccer
  • 899
  • 2
  • 10
  • 25
2
votes
0 answers

Last observation carried forward / ignore nulls in lag

How can I imitate the LOCF behavior induced by lag(x) ignore nulls on, e.g., Redshift, in Presto? Take this sample data: select * from ( values (7369, null), (7499, 300), (7521, 500), (7566, null), (7654, 1400), (7698, null), …
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
1
vote
1 answer

Plot imputed values

I was asked to have a dataset imputed with both the LOCF and the NOCB methods by using na.locf() function from zoo package and I'm trying now plotting both the observed and the imputed values. The dataset I'm working is the following…
12666727b9
  • 1,133
  • 1
  • 8
  • 22
1
2 3 4