9

This question has been asked for excel. How to automatically insert a blank row after a group of data

I would like to know if there is a function for doing the same in R.

example:

group <- c("a","b","b","c","c","c","d","d","d","d")
xvalue <- c(16:25)
yvalue <- c(1:10)
df <- data.frame(cbind(group,xvalue,yvalue))
   group xvalue yvalue
1      a     16      1
2      b     17      2
3      b     18      3
4      c     19      4
5      c     20      5
6      c     21      6
7      d     22      7
8      d     23      8
9      d     24      9
10     d     25     10

I would like to have a blank row after each group so that I can check the entries manually

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

thanks

Community
  • 1
  • 1
Rfan
  • 722
  • 6
  • 11

2 Answers2

13

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
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • 1
    @ sven: why transform all variables to text? Also could make a solution with tidyverse? – xhr489 Feb 18 '19 at 23:10
  • @David How do you want to represent "empty rows" without text? – Sven Hohenstein Feb 19 '19 at 06:38
  • @ sven. Sorry for late reply. For every group I would like to insert a blank row and fill the values with e.g, an average of the group. – xhr489 Feb 19 '19 at 08:05
  • @David Of course, you insert values into the "blank" rows, but this is not the solution the OP was looking for. – Sven Hohenstein Feb 19 '19 at 12:46
  • @ svend. Okay... I have a new question about encoding, could you look at it? [link](https://stackoverflow.com/questions/54764918/encoding-problem-correct-special-characters-after-reading-in-the-table) – xhr489 Feb 19 '19 at 12:49
7

Sven has answered exactly what you've asked for, but I think what you want to do is a generally bad idea.

If visual separation is all that you are hoping to achieve, I would recommend just using split:

split(df, df$group)
# $a
#   group xvalue yvalue
# 1     a     16      1
#
# $b
#   group xvalue yvalue
# 2     b     17      2
# 3     b     18      3
#
# $c
#   group xvalue yvalue
# 4     c     19      4
# 5     c     20      5
# 6     c     21      6
#
# $d
#    group xvalue yvalue
# 7      d     22      7
# 8      d     23      8
# 9      d     24      9
# 10     d     25     10

This has the advantages of (1) visual separation, (2) easy indexing, (3) no conversion of your data, and (4) allowing you to run the same functions on different subsets of your data.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • @Anando I agree with you and thank you for telling me about `split` function. In this case I have more than 500 groups and need to manually inspect each one against the original print document. The choice is to export to excel and then run the excel macro to split or split in r and export to excel. – Rfan Nov 28 '13 at 19:33