在R中基于两个因子级别插入行

时间:2018-10-13 11:54:47

标签: r

我有一个带有四个变量的数据帧(df),其中两个是因子var1和var2。 var1和var2每个都有三个级别。

数据帧中不存在var1和var2的某些组合,例如,对于var1级别“ slow”,不存在var2级别“ 4或5”。

我想将那些缺少的组合行添加到我的数据框(dfgoal),并将这些行的var3和var4设置为0。

我发现最好在最好的时候添加行,而且不知道如何实现。任何帮助将非常感激!

# Starting point 
df <- data.frame(var1=c("fast","fast","fast","medium","slow","slow"),
                 var2=c("1 or 2","3","4 or 5","3","1 or 2","3"),
                 var3_freq=c(22,56,22,100,36,64),
                 var4_n=c(10,26,10,2,5,9))
df$var1 <- as.factor(df$var1)
df$var2 <- as.factor(df$var2)

# Goal
dfgoal <- data.frame(var1=c("1 or 2","3","4 or 5","1 or 2","3","4 or 5","1 or 2","3","4 or 5"),
                 var2=c("fast","fast","fast","medium","medium","medium","slow","slow","slow"),
                 var3_freq=c(22,56,22,0,100,0,36,64,0),
                 var4_n=c(10,26,10,0,2,0,5,9,0))

2 个答案:

答案 0 :(得分:0)

您可以使用filter()中的complete()

tidyr

答案 1 :(得分:0)

无需加载外部库的简单解决方案:

    var1   var2 var3_freq var4_n
1   fast 1 or 2        22     10
2   fast      3        56     26
3   fast 4 or 5        22     10
4 medium      3       100      2
5   slow 1 or 2        36      5
6   slow      3        64      9
7 medium 1 or 2         0      0
8 medium 4 or 5         0      0
9   slow 4 or 5         0      0

代码

new <- data.frame(var1 = c("medium", "medium", "slow"),
                  var2 = c("1 or 2", "4 or 5", "4 or 5"),
                  var3_freq = c(0, 0, 0),
                  var4_n = c(0, 0, 0))
rbind(df, new)

数据

df <- data.frame(var1=c("fast","fast","fast","medium","slow","slow"),
                 var2=c("1 or 2","3","4 or 5","3","1 or 2","3"),
                 var3_freq=c(22,56,22,100,36,64),
                 var4_n=c(10,26,10,2,5,9))
df$var1 <- as.factor(df$var1)
df$var2 <- as.factor(df$var2)