根据标识符输入缺失值 [英] Inpute missing value based on identifier

查看:51
本文介绍了根据标识符输入缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢根据某个变量的值与匹配的索引配对来填充它的缺失值.示例(第一个列是索引;第二个列是值).

I like to fill in missing values for some variable based on its value paired with a matching index. Example (First col is the index; second col is the values).

mat <- cbind(c(1,1,2,2,3,3,4,4,4), c(4.3, NA, 2.4, NA, 3.8, NA, 1.2, NA, NA))

newmat <- cbind(c(1,1,2,2,3,3,4,4,4), c(4.3, 4.3, 2.4, 2.4, 3.8, 3.8, 1.2, 1.2, 1.2))

推荐答案

我们可以使用fill

library(dplyr)
library(tidyr)
mat %>% 
  as_tibble %>% 
  group_by(V1) %>% 
  fill(V2, .direction = 'updown')

这篇关于根据标识符输入缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆