Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...