Home > Back-end >  how to modify the combinations table (that made by expand.grid() function) in r?
how to modify the combinations table (that made by expand.grid() function) in r?

Time:05-24

I made small, reproducible example, but my real data is really huge...and have lots of levels.

roomnumber <- c(1,2,3)

color <- c('red','blue','green')

weight <- c(0.1,0.3,0.5)

data <- expand.grid(roomnumber,color,weight)
names(data) <- c('roomnumber','color','weight')

so, the table is as follows. it contains every combinations.

 roomnumber color weight
1           1   red    0.1
2           2   red    0.1
3           3   red    0.1
4           1  blue    0.1
5           2  blue    0.1
6           3  blue    0.1
7           1 green    0.1
8           2 green    0.1
9           3 green    0.1
10          1   red    0.3
11          2   red    0.3
12          3   red    0.3
13          1  blue    0.3
14          2  blue    0.3
15          3  blue    0.3
16          1 green    0.3
17          2 green    0.3
18          3 green    0.3
19          1   red    0.5
20          2   red    0.5
21          3   red    0.5
22          1  blue    0.5
23          2  blue    0.5
24          3  blue    0.5
25          1 green    0.5
26          2 green    0.5
27          3 green    0.5

however, in this data, I want something more. when the "color" variable is "blue", I want to add more combination

specifically, the weight is either 0.1, 0.3, or 0.5 for this combinations but when the "color" is "blue", I want a weight to be 0.1, 0.3, 0.5 AND 0.7


weight_2 <- c(0.1, 0.3, 0.5, 0.7) 

how do I edit this?

(I can easily do manually with this minimal example, but my real data is so huge that it is hard to do manually)

CodePudding user response:

There are a couple of ways, both slightly inefficient (though that may be unavoidable).

  1. Add it for all, then remove 0.7 from non-blue rows.

    data <- expand.grid(roomnumber = 1:3, color = c("red", "blue", "green"), weight = c(0.1, 0.3, 0.5, 0.7))
    data <- subset(data, color == "blue" | weight != 0.7)
    data
    #    roomnumber color weight
    # 1           1   red    0.1
    # 2           2   red    0.1
    # 3           3   red    0.1
    # 4           1  blue    0.1
    # 5           2  blue    0.1
    # 6           3  blue    0.1
    # 7           1 green    0.1
    # 8           2 green    0.1
    # 9           3 green    0.1
    # 10          1   red    0.3
    # 11          2   red    0.3
    # 12          3   red    0.3
    # 13          1  blue    0.3
    # 14          2  blue    0.3
    # 15          3  blue    0.3
    # 16          1 green    0.3
    # 17          2 green    0.3
    # 18          3 green    0.3
    # 19          1   red    0.5
    # 20          2   red    0.5
    # 21          3   red    0.5
    # 22          1  blue    0.5
    # 23          2  blue    0.5
    # 24          3  blue    0.5
    # 25          1 green    0.5
    # 26          2 green    0.5
    # 27          3 green    0.5
    # 31          1  blue    0.7
    # 32          2  blue    0.7
    # 33          3  blue    0.7
    
  2. rbind the additional blue-only rows.

    data <- expand.grid(roomnumber = 1:3, color = c("red", "blue", "green"), weight = c(0.1, 0.3, 0.5))
    data <- rbind(data, expand.grid(roomnumber = 1:3, color = "blue", weight = 0.7))
    data
    #    roomnumber color weight
    # 1           1   red    0.1
    # 2           2   red    0.1
    # 3           3   red    0.1
    # 4           1  blue    0.1
    # 5           2  blue    0.1
    # 6           3  blue    0.1
    # 7           1 green    0.1
    # 8           2 green    0.1
    # 9           3 green    0.1
    # 10          1   red    0.3
    # 11          2   red    0.3
    # 12          3   red    0.3
    # 13          1  blue    0.3
    # 14          2  blue    0.3
    # 15          3  blue    0.3
    # 16          1 green    0.3
    # 17          2 green    0.3
    # 18          3 green    0.3
    # 19          1   red    0.5
    # 20          2   red    0.5
    # 21          3   red    0.5
    # 22          1  blue    0.5
    # 23          2  blue    0.5
    # 24          3  blue    0.5
    # 25          1 green    0.5
    # 26          2 green    0.5
    # 27          3 green    0.5
    # 28          1  blue    0.7
    # 29          2  blue    0.7
    # 30          3  blue    0.7
    
  • Related