Insert a blank line after each data group

This question has been asked for Excel. How to automatically insert a blank line after a data group

I would like to know if there is a function to do 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 want to have an empty line after each group so that I can manually check the entries

  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

+4
r
source share
2 answers

First convert all columns to character vectors:

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

Then you can create the output you are 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 
+8
source share

Sven answered exactly what you asked, but I think what you want to do is a bad idea at all.

If visual separation is all you hope to achieve, I would recommend 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) the lack of conversion of your data, and (4) allows you to perform the same functions on different subsets of your data.

+5
source share

All Articles