使用 dplyr 按组将 NA 替换为上一个或下一个值 [英] Replace NA with previous or next value, by group, using dplyr
本文介绍了使用 dplyr 按组将 NA 替换为上一个或下一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按日期降序排列的数据框.
I have a data frame which is arranged by descending order of date.
ps1 = data.frame(userID = c(21,21,21,22,22,22,23,23,23),
color = c(NA,'blue','red','blue',NA,NA,'red',NA,'gold'),
age = c('3yrs','2yrs',NA,NA,'3yrs',NA,NA,'4yrs',NA),
gender = c('F',NA,'M',NA,NA,'F','F',NA,'F')
)
我希望用以前的值来估算(替换)NA 值并按用户 ID 分组如果用户 ID 的第一行具有 NA,则替换为该用户 ID 组的下一组值.
I wish to impute(replace) NA values with previous values and grouped by userID In case the first row of a userID has NA then replace with the next set of values for that userid group.
我正在尝试使用 dplyr 和 zoo 包这样的东西......但它不起作用
I am trying to use dplyr and zoo packages something like this...but its not working
cleanedFUG <- filteredUserGroup %>%
group_by(UserID) %>%
mutate(Age1 = na.locf(Age),
Color1 = na.locf(Color),
Gender1 = na.locf(Gender) )
我需要这样的结果 df:
I need result df like this:
userID color age gender
1 21 blue 3yrs F
2 21 blue 2yrs F
3 21 red 2yrs M
4 22 blue 3yrs F
5 22 blue 3yrs F
6 22 blue 3yrs F
7 23 red 4yrs F
8 23 red 4yrs F
9 23 gold 4yrs F
推荐答案
require(tidyverse) #fill is part of tidyr
ps1 %>%
group_by(userID) %>%
fill(color, age, gender) %>% #default direction down
fill(color, age, gender, .direction = "up")
这给了你:
Source: local data frame [9 x 4]
Groups: userID [3]
userID color age gender
<dbl> <fctr> <fctr> <fctr>
1 21 blue 3yrs F
2 21 blue 2yrs F
3 21 red 2yrs M
4 22 blue 3yrs F
5 22 blue 3yrs F
6 22 blue 3yrs F
7 23 red 4yrs F
8 23 red 4yrs F
9 23 gold 4yrs F
这篇关于使用 dplyr 按组将 NA 替换为上一个或下一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文