[英]How to fill in blanks based off another column's values in R
Here is the code and resulting dataframe这是代码和生成的 dataframe
category <- c("East","BLANK","NorthEast","BLANK","BLANK")
subcat <- c("East","North","SW","NE","SE")
data1 <- as.data.frame(category)
data1$subcat <- subcat
**Category** **Subcat**
East East
BLANK North
NorthEast SW
BLANK NE
BLANK SE
The East category contains subcat East and North. East 类别包含 subcat East 和 North。 The NorthEast category contains subcat SW,NE,SE. NorthEast 类别包含 subcat SW、NE、SE。 As you can see there are blanks for each category.如您所见,每个类别都有空白。 How would I make so the 2nd value in Category is East and 4th and 5th row is North East?我将如何使类别中的第二个值是东,第 4 和第 5 行是东北? I have many more rows in the actual data so a way to do this would be helpful.我在实际数据中有更多行,所以这样做的方法会很有帮助。
The result should be结果应该是
**Category** **Subcat**
East East
*East* North
NorthEast SW
*NorthEast* NE
*NorthEast* SE
We could convert the 'BLANK' to NA
and use fill
我们可以将“空白”转换为NA
并使用fill
library(tidyr)
library(dplyr)
data1 %>%
na_if( "BLANK") %>%
fill(category)
-output -输出
category subcat
1 East East
2 East North
3 NorthEast SW
4 NorthEast NE
5 NorthEast SE
Here is a dplyr
only solution: First we group by every string that is not BLANK
, then replace all group members with first value:这是一个dplyr
唯一的解决方案:首先我们按每个不是BLANK
的字符串分组,然后用第一个值替换所有组成员:
data1 %>%
group_by(x = cumsum(category != "BLANK")) %>%
mutate(category = first(category)) %>%
ungroup() %>%
select(-x)
category subcat
<chr> <chr>
1 East East
2 East North
3 NorthEast SW
4 NorthEast NE
5 NorthEast SE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.