How can I use merge to have all time data?

I am trying to change data in which all objects matter for all possible times (months). This is what I am trying;

Class  Value  month
  A      10     1
  A      12     3
  A      9      12
  B      11     1
  B      10     8

From the above data, I want to get the following data:

Class  Value  month
  A      10     1
  A      NA     2
  A      12     3
  A      NA     4
        ....
  A      9      12
  B      11     1
  B      NA     2
        ....
  B      10     8
  B      NA     9
        ....
  B      NA     12

So, I want to have all possible cells with a month from 1 to 12; How can i do this? I'm trying to use the merge function now, but I appreciate any other approaches.

+4
source share
3 answers

We can use tidyverse

library(tidyverse)
df1 %>%
   complete(Class, month = min(month):max(month)) %>% 
   select_(.dots = names(df1)) %>%  #if we need to be in the same column order
   as.data.frame() #if needed to convert to 'data.frame'
+4
source

The R database uses merge(where dfis your data):

res <- data.frame(Class=rep(levels(df$Class), each=12), value=NA, month=1:12)
merge(df, res, by = c("Class", "month"), all.y = TRUE)[,c(1,3,2)]

   # Class Value month
# 1      A    10     1
# 2      A    NA     2
# 3      A    12     3
# 4      A    NA     4
# 5      A    NA     5
# 6      A    NA     6
# 7      A    NA     7
# 8      A    NA     8
# 9      A    NA     9
# 10     A    NA    10
# 11     A    NA    11
# 12     A     9    12
# 13     B    11     1
# 14     B    NA     2
# 15     B    NA     3
# 16     B    NA     4
# 17     B    NA     5
# 18     B    NA     6
# 19     B    NA     7
# 20     B    10     8
# 21     B    NA     9
# 22     B    NA    10
# 23     B    NA    11
# 24     B    NA    12

df <- structure(list(Class = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), Value = c(10L, 12L, 9L, 11L, 10L), month = c(1L, 
3L, 12L, 1L, 8L)), .Names = c("Class", "Value", "month"), class = "data.frame", row.names = c(NA, 
-5L))
+3
source

@akrun , NA 0, :

library(dplyr)
library(tidyr)

df1 %>%
complete(Class, month = min(month):max(month)) %>%
mutate(Value = ifelse(is.na(Value),0,Value))
+1

Source: https://habr.com/ru/post/1674456/


All Articles