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
Using R to Download Zipped Data File, Extract, and Import .Csv
Convert Quarter/Year Format to a Date
Si Prefixes in Ggplot2 Axis Labels
Convert List to Data Frame While Keeping List-Element Names
Changing Format of Some Axis Labels in Ggplot2 According to Condition
Dealing with Spaces and "Weird" Characters in Column Names with Dplyr::Rename()
How to Calculate Mean of All Columns, by Group
Ggplot2: Using Gtable to Move Strip Labels to Top of Panel for Facet_Grid
R Shiny, How to Make Datatable React to Checkboxes in Datatable
How to Change Angle of Line in Customized Legend in Ggplot2
Remove Text After Final Period in String
R Programming: Cache the Inverse of a Matrix
Numbers as Column Names of Data Frames
Union of Intersecting Vectors in a List in R
How to Produce a Heatmap with Ggplot2
How to Merge Two Data Frames on Common Columns in R with Sum of Others
Weird Error in R When Importing (64-Bit) Integer with Many Digits