Home > Blockchain >  Add a blank row after each unique value of a column in a dataframe in R
Add a blank row after each unique value of a column in a dataframe in R

Time:12-09

I was wondering if it might be possible to add one blank row after each unique value of study in my data below?

My Desired_output is below.

Please note that this is a toy data. A functional answer is highly appreciated.

data <- data.frame(study=c(rep(1,2),2:3), year=c(rep(2001,2),2002:2003))

Desired_output =
" study year
1     1 2001
2     1 2001
             # <- Blank row
3     2 2002
             # <- Blank row
4     3 2003"

CodePudding user response:

You can use group_split to split your data by group into a list of data frames. Then map a function over each list element and stack their output back into a data frame using map_dfr.

library(dplyr)
library(tibble)
library(purrr)

data %>% 
  group_split(study) %>% 
  map_dfr(~ add_row(.x, .after = Inf))

Output

  study  year
  <dbl> <dbl>
1     1  2001
2     1  2001
3    NA    NA
4     2  2002
5    NA    NA
6     3  2003
7    NA    NA

CodePudding user response:

Here is a base R solution:

data_new <- as.data.frame(lapply(data, as.character), stringsAsFactors = FALSE)
head(do.call(rbind, by(data_new, data$study, rbind, "")), -1)
    study year
1.1     1 2001
1.2     1 2001
1.3           
2.3     2 2002
2.2           
3.4     3 2003

CodePudding user response:

Group by a new column equal to study and then add a row to the end of each group using group_modify. Finally remove study2 and the last row.

library(dplyr)

data %>%
 group_by(study2 = study) %>%
 group_modify(~ add_row(.)) %>%
 ungroup %>%
 select(-study2) %>%
 slice(-n())

giving:

# A tibble: 7 x 2
  study  year
  <dbl> <dbl>
1     1  2001
2     1  2001
3    NA    NA
4     2  2002
5    NA    NA
6     3  2003
  • Related