Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

r - Find missing month after grouping with dplyr

I have a data frame with two columns that I am grouping by with dplyr, a column of months (as numerics, e.g. 1 through 12), and several columns with statistical data following that (values unimportant). An example:

ID_1   ID_2   month  st1    st2
1      1      1      0.5    0.2
1      1      2      0.7    0.9
1      1      3      1.1    1.7
1      1      4      2.6    0.8
1      1      5      1.8    1.3
1      1      6      2.1    2.2
1      1      7      0.5    0.2
1      1      8      0.7    0.9
1      1      9      1.1    1.7
1      1      10     2.6    0.8
1      1      11     1.8    1.3
1      1      12     2.1    2.2
1      2      1      0.5    0.2
1      2      2      0.7    0.9
1      2      3      1.1    1.7
1      2      4      2.6    0.8
1      2      5      1.8    1.3
1      2      6      2.1    2.2
1      2      7      0.5    0.2
1      2      9      1.1    1.7
1      2      10     2.6    0.8
1      2      11     1.8    1.3
1      2      12     2.1    2.2

For the second grouping (ID_1 = 1 and ID_2 = 2), there is a month missing from the data (month = 8). Is there a way I can find this month and insert a row with the correct ID_1 and ID_2 values, the missing month value, and NA values for the rest of the columns? I've been playing around with this using dplyr functions and can't seem to figure it out, perhaps there is even a non-dplyr solution out there as well.

PS: If it helps, each unique grouping of ID_1 and ID_2 will have no more than 1 month missing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Expand grid to make all combos of groups, then merge:

# make reference with all needed rows
ref <- data.frame(expand.grid(unique(df1$ID_1),
                              unique(df1$ID_2),
                              1:12))
colnames(ref) <- colnames(df1)[1:3]

# them merge with all TRUE
res <- merge(df1, ref, all = TRUE)

# to check output, show only month = 8
res[ res$month == 8, ]
#    ID_1 ID_2 month st1 st2
# 8     1    1     8 0.7 0.9
# 20    1    2     8  NA  NA

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...