amisos55
amisos55

Reputation: 1979

Adding a row based on a condition in R

I am trying to add a row based on a condition into my dataset. Here is a snapshot of my dataset.

library(DataCombine)
id <-       c(1,1, 2,2, 3,3)
sequence <- c(NA,NA, NA,NA, NA,NA)
score <-    c(-0.25,0.25, -0.37,0.37, 0,0)

data <- data.frame(id, sequence,score)

> data
  id sequence score
1  1       NA -0.25
2  1       NA  0.25
3  2       NA -0.37
4  2       NA  0.37
5  3       NA  0.00
6  3       NA  0.00

When there is a pair of -/+ score, I need to add a row to the top of that pair with a sequence number of 0,1,2 and a score of 0. I tried the procedure below but had an error message. If the score is 0, then only add a sequence number of (0,1). The code below is incomplete, but this how I

row <- nrow(data)
      for (i in 1:nrow) {

  new.id <- data[i,1]
  new.row <- c(new.id,0, 0) 

  if(data$score < 0) {
    InsertRow(data, NewRow = new.row, RowNum = new.id)
  }
}

Error in 1:nrow : NA/NaN argument

Eventually, I would like to get a dataset like this below:

> data
  id sequence score
1  1       0  0.00
1  1       1 -0.25
2  1       2  0.25
3  2       0  0.00
3  2       1 -0.37
4  2       2  0.37
5  3       0  0.00
6  3       1  0.00

Any thoughts? Thanks

Upvotes: 0

Views: 66

Answers (1)

akrun
akrun

Reputation: 886938

Here is an option

library(dplyr)
library(tibble)
library(purrr)
data %>% 
   group_by(id) %>%
   summarise(n = n_distinct(sign(score)), 
             sequence = list(if(n == 2) 0:2 else 0:1), 
             score = list(if(n==2) c(0, score) else score)) %>% 
   unnest(cols = c(sequence, score)) %>%
   select(-n)
# A tibble: 8 x 3
#     id sequence score
#  <dbl>    <int> <dbl>
#1     1        0  0   
#2     1        1 -0.25
#3     1        2  0.25
#4     2        0  0   
#5     2        1 -0.37
#6     2        2  0.37
#7     3        0  0   
#8     3        1  0   

Upvotes: 1

Related Questions