Insert a blank row after each group of data

First transform all columns to character vectors:

df_new <- as.data.frame(lapply(df, as.character), stringsAsFactors = FALSE)

Then you can create the output you’re looking for:

head(do.call(rbind, by(df_new, df$group, rbind, "")), -1 )

#      group xvalue yvalue
# a.1      a     16      1
# a.2                     
# b.2      b     17      2
# b.3      b     18      3
# b.31                    
# c.4      c     19      4
# c.5      c     20      5
# c.6      c     21      6
# c.41                    
# d.7      d     22      7
# d.8      d     23      8
# d.9      d     24      9
# d.10     d     25     10

Leave a Comment