填充数据框中的缺失行 [英] fill missing rows in a dataframe
本文介绍了填充数据框中的缺失行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数据框
I have a data frame shown below
Hair Eye Freq
1 Black Brown 32
2 Brown Brown 53
3 Red Brown 10
4 Blond Brown 3
5 Red Blue 10
6 Blond Blue 30
7 Black Hazel 10
8 Blond Hazel 5
在上述数据框中出现4种发色的频率黑色,棕色,红色和金色
在不同的眼睛颜色中标记为棕色,蓝色和淡褐色
。但是,我想为每种眼睛的颜色填充丢失的头发颜色频率,以使其产生如下数据帧。
In the above data frame frequency of 4 hair colors Black, Brown, Red and Blond
are noted across different eye colors Brown, Blue and Hazel
. However, I would like to fill in the missing hair color frequency for respective eye color so that it results in a data frame as below. Any help is appreciated.
Hair Eye Freq
1 Black Brown 32
2 Brown Brown 53
3 Red Brown 10
4 Blond Brown 3
5 Black Blue 0
6 Brown Blue 0
7 Red Blue 10
8 Blond Blue 30
9 Black Hazel 10
10 Brown Hazel 0
11 Red Hazel 0
12 Blond Hazel 5
推荐答案
如果我们使用的是 R
,则一个选项是 complete
来自 tidyr
If we are using R
, one option is complete
from tidyr
library(tidyr)
complete(df1, Hair, Eye, fill = list(Freq = 0)) %>%
arrange(factor(Eye, levels = unique(df1$Eye)), factor(Hair, levels = unique(df1$Hair)))
# A tibble: 12 × 3
# Hair Eye Freq
# <chr> <chr> <dbl>
#1 Black Brown 32
#2 Brown Brown 53
#3 Red Brown 10
#4 Blond Brown 3
#5 Black Blue 0
#6 Brown Blue 0
#7 Red Blue 10
#8 Blond Blue 30
#9 Black Hazel 10
#10 Brown Hazel 0
#11 Red Hazel 0
#12 Blond Hazel 5
这篇关于填充数据框中的缺失行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文