将值为“0”的行添加到缺少数据的数据框

时间:2016-09-16 19:13:28

标签: r dataframe

我是R的新手,目前正在使用相当大的数据帧。基本上我想要做的就是这样:

   Year Sample Species Catch
1  2016      1       a     9
2  2016      1       b     5
3  2016      1       c    13
4  2016      1       d     2
5  2016      1       e     4
6  2016      1       f    13
7  2016      2       a     7
8  2016      2       c     5
9  2016      2       f     6
10 2016      2       g     2

进入这个:

   Year Sample Species Catch
1  2016      1       a     9
2  2016      1       b     5
3  2016      1       c    13
4  2016      1       d     2
5  2016      1       e     4
6  2016      1       f    13
7  2016      1       g     0
8  2016      1       h     0
9  2016      1       i     0
10 2016      1       j     0
11 2016      1       k     0
12 2016      2       a     7
13 2016      2       b     0
14 2016      2       c     5
15 2016      2       d     0
16 2016      2       e     0
17 2016      2       f     6
18 2016      2       g     2
19 2016      2       h     0
20 2016      2       i     0
21 2016      2       j     0
22 2016      2       k     0

也就是说,有一定数量的物种(a到k),并且在“样本”中没有该物种的记录,我希望记录显示为0.

谢谢!

1 个答案:

答案 0 :(得分:1)

这个怎么样?

all.species <- c('a','b', 'c','d','e','f','g','h','i','j','k')
samples <- split(df, df$Sample)
new.df <- NULL
for (sample in samples) {
  missing.species <- setdiff(all.species, unique(sample$Species))
  sample <- rbind(sample, data.frame(Year=unique(sample$Year), 
                                     Sample=unique(sample$Sample), 
                                     Species=missing.species, Catch=0))
  new.df <- rbind(new.df, sample[order(sample$Species),])
}
new.df

带输出

Year Sample Species Catch
1   2016      1       a     9
2   2016      1       b     5
3   2016      1       c    13
4   2016      1       d     2
5   2016      1       e     4
6   2016      1       f    13
7   2016      1       g     0
8   2016      1       h     0
9   2016      1       i     0
10  2016      1       j     0
11  2016      1       k     0
72  2016      2       a     7
51  2016      2       b     0
82  2016      2       c     5
61  2016      2       d     0
71  2016      2       e     0
92  2016      2       f     6
102 2016      2       g     2
81  2016      2       h     0
91  2016      2       i     0
101 2016      2       j     0
111 2016      2       k     0