Extend an Irregular Sequence and Add Zeros to Missing Values

r - Fill in missing years in Data frame

We may use complete on the 'counts' data

library(tidyr)
complete(counts, year = 1990:1999, fill = list(freq = 0))

Identify missing values in a sequence / perform asymmetric difference between two lists

Another possible solution

 setdiff(full.list,data.list)

Data.table replace sequence of values with NA

Try to use

dt[2:5, (specific_column) := NA]

r Copy values from a cell above the missing value

There is a function na.locf (NA last observation carried forward) to do this in the package zoo:

> zoo::na.locf(df)
Col1 Col2
1 0 1.0000
2 1 0.9971
3 4 0.9971
4 6 0.9971
5 7 0.9971
6 14 0.9971
7 18 0.9951
8 22 0.9951
9 25 0.9951
10 46 0.9941
11 57 0.9941
12 59 0.9941
13 60 0.9921

Assigning values based on fractions of data

You can do this by generating a vector of "Red" and "Blue" to select as the replacement when needed.

## Generate some random data with missing values
set.seed(2017)
a = sample(c("Red", "Blue"), 20, replace=TRUE)
a = ifelse(runif(20, 0, 1) < 0.12, NA, a)

## Now replace missing
a = ifelse(is.na(a),
sample(c("Red", "Blue"), length(a), replace=TRUE, prob=c(0.5,0.5)), a)

Insert rows for missing dates/times

I think the easiest thing ist to set Date first as already described, convert to zoo, and then just set a merge:

df$timestamp<-as.POSIXct(df$timestamp,format="%m/%d/%y %H:%M")

df1.zoo<-zoo(df[,-1],df[,1]) #set date to Index

df2 <- merge(df1.zoo,zoo(,seq(start(df1.zoo),end(df1.zoo),by="min")), all=TRUE)

Start and end are given from your df1 (original data) and you are setting by - e.g min - as you need for your example. all=TRUE sets all missing values at the missing dates to NAs.



Related Topics



Leave a reply



Submit