1

I'm basically doing what is described in this question, however I'm trying to maintain the original order of the month column. A roundabout way would be to add a leading zero to the single digit numbers. I'm trying to find a way without doing that.

Current code:

library(dplyr)
df <- structure(list(parameters = c("temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp", "temp"), month = c("2", "2", "2", "5", "5", "5", "8", "8", "8", "11", "11", "11", "annual", "annual", "annual")), class = "data.frame", row.names = c(NA, -15L))
do.call(bind_rows, by(df, df[ ,c("month", "parameters")], rbind, ""))

The by function looks like it coerces your defined indices to factors, and converting month to a factor reveals that it makes the levels in that order: 11, 2, 5, 8, annual. If it were numeric, it would order them correctly, but with annual included, that column must be as a character.

If I convert it to a factor first, and order the levels, my code will insert NAs.

df$month <- ordered(df$month, levels = c("2", "5", "8", "11", "annual"))
do.call(bind_rows, by(df, df[ ,c("month", "parameters")], rbind, ""))

Current output:

   parameters  month
1        temp     11
2        temp     11
3        temp     11
4                   
5        temp      2
6        temp      2
7        temp      2
8                   
9        temp      5
10       temp      5
11       temp      5
12                  
13       temp      8
14       temp      8
15       temp      8
16                  
17       temp annual
18       temp annual
19       temp annual
20                  

Desired output:

   parameters month
1        temp     2
2        temp     2
3        temp     2
4                  
5        temp     5
6        temp     5
7        temp     5
8                  
9        temp     8
10       temp     8
11       temp     8
12                 
13       temp    11
14       temp    11
15       temp    11
16                 
17       temp annual
18       temp annual
19       temp annual
20                  
Anonymous coward
  • 2,061
  • 1
  • 16
  • 29

2 Answers2

1

The issue is that after the column 'month' got changed to an ordered factor, "" is not specified as one of the levels. So, naturally any value that is not a level is treated as missing value and hence we get the NA. Correction can be done at the earlier step by including the "" as one of the levels

df$month <- ordered(df$month, levels = c("2", "5", "8", "11", "annual", ""))

NOTE: The order of the "" is not clear. So, it is specified as the last level

akrun
  • 874,273
  • 37
  • 540
  • 662
1

There is an alternative approach, which uses 's incarnation of the rbind() function to append a blank line after each group:

library(data.table)
setDT(df)[, rbind(.SD, data.table(parameters = "")), by = month]
     month parameters
 1:      2       temp
 2:      2       temp
 3:      2       temp
 4:      2           
 5:      5       temp
 6:      5       temp
 7:      5       temp
 8:      5           
 9:      8       temp
10:      8       temp
11:      8       temp
12:      8           
13:     11       temp
14:     11       temp
15:     11       temp
16:     11           
17: annual       temp
18: annual       temp
19: annual       temp
20: annual

The order of groups is kept. The grouping variable month appears in front of each line. This approach can also be used to intersperse an arbitrary number of blank lines if required:

n_blank <- 2L
setDT(df)[, rbind(.SD, data.table(parameters = rep("", n_blank))), by = month]
     month parameters
 1:      2       temp
 2:      2       temp
 3:      2       temp
 4:      2           
 5:      2           
 6:      5       temp
 7:      5       temp
 8:      5       temp
 9:      5           
10:      5           
11:      8       temp
12:      8       temp
13:      8       temp
14:      8           
15:      8           
16:     11       temp
17:     11       temp
18:     11       temp
19:     11           
20:     11           
21: annual       temp
22: annual       temp
23: annual       temp
24: annual           
25: annual           
     month parameters
Uwe
  • 41,420
  • 11
  • 90
  • 134