1
votes

I have a data frame that is something like this:

time type count
1   -2    a     1
2   -1    a     4
3    0    a     6
4    1    a     2
5    2    a     5
6    0    b     3
7    1    b     7
8    2    b     2

I want to create a new data frame that takes type 'b' and creates the full time series by filling in zeroes for count. It should look like this:

time type count
1   -2    b     0
2   -1    b     0
3    0    b     3
4    1    b     7
5    2    b     2

I can certainly subset(df, df$type = 'b') and then hack the beginning and rbind, but I want it to be more dynamic just in case the time vector changes.

2

2 Answers

1
votes

We can use complete from tidyr to get the full 'time' for all the unique values of 'type' and filter the value of interest in 'type'.

library(tidyr)
library(dplyr)
val <- "b"
df1 %>% 
    complete(time, type, fill=list(count=0)) %>%
    filter(type== val)
#   time  type count
#  <int> <chr> <dbl>
#1    -2     b     0
#2    -1     b     0
#3     0     b     3
#4     1     b     7
#5     2     b     2
0
votes

With base R:

df1 <- data.frame(time=df[df$type == 'a',]$time, type='b', count=0)
df1[match(df[df$type=='b',]$time, df1$time),]$count <- df[df$type=='b',]$count 

df1
    time type count
1   -2    b     0
2   -1    b     0
3    0    b     3
4    1    b     7
5    2    b     2