Search code examples
rloopsgroupingsapplyrbind

Using rbind to add 0 values during a loop


I am having trouble with coding a loop correctly for adding rows to a small dataset.

Say I have the following dataframe called "colors" :

color   count   group
Blue      3       1
Blue      2       2
Red       2       2
Green     1       1

Now what I need is to add 0 values for each column so that all groups have each color, aka it should look like:

color   count   group
Blue      3       1
Blue      2       2
Red       2       2
Green     1       1
Red       0       1 
Green     0       2

The thing I tried to do that came closest to the solution I want is:

color.u <- unique(colors$color)

color.z<- function(x){
  if(x %in% colors$color[colors$group == "1"] == F ) {
    rbind(colors, c(x, 0, "1"))
    }
if(x %in% colors$color[colors$group == "2"] == F ) {
    rbind(colors, c(x, 0, "2"))
    }
}

sapply(color.u, function(x) color.z(x))

What this function returns is the whole dataset repeatedly with only one of the two zeroed values in the end. I understand why it is a mistake and I am sure the solution is easy but I am at a loss on how to correct it. Any suggestions?

Thanks!

A.


Solution

  • Using base R

    df1 <- structure(list(color = c("Blue", "Blue", "Red", "Green"),
                          count = c(3, 2, 2, 1),
                          group = c(1L, 2L, 2L, 1L)),
                     row.names = c(NA, -4L),
                     class = "data.frame")
    
    df2 <- expand.grid(color = unique(df1$color), group = unique(df1$group))
    df2 <- merge(df2, df1, all = TRUE)
    df2$count[is.na(df2$count)] <- 0
    
      color group count
    1  Blue     1     3
    2  Blue     2     2
    3   Red     1     0
    4   Red     2     2
    5 Green     1     1
    6 Green     2     0