[英]Fill missing values in data.frame across columns
Normally people want to fill missing values up or down a column. 通常,人们希望在列的上下填充缺失值。 I would like to fill across, left or right.
我想填写左右或左右。
# all character data.frame, except 1st column
df <- data.frame(a = 1:4,
b = c('row1', 'row2', 'row3', 'row4'),
c = paste(9:12),
d = paste(13:16))
# remove a few values
df[2,2] <- df[3,3] <- df[4,2] <- NA
> df
a b c d
1 1 row1 9 13
2 2 <NA> 10 14
3 3 row3 <NA> 15
4 4 <NA> 12 16
# fill down. This is straighforward and works as expected.
df%>%fill(names(.), .direction='down')
How do I fill across??? 我如何填写???
# this doesn't work
df%>%fill(names(.), direction='right')
Lets assume we coerce to character
if the fill value does not match the type of the missing value. 假设填充值与缺失值的类型不匹配,我们强制转换为
character
。 But only for the columns that require coercion. 但是仅适用于需要强制的列。 So column
a
should stay numeric
因此,
a
列应保持numeric
This would be the equivalent of the right
variant: 这将等同于
right
变体:
library(tidyverse)
df %>%
rowid_to_column %>%
gather(key, val, -rowid) %>%
arrange(rowid) %>%
fill(val) %>%
spread(key, val) %>% select(-rowid)
Basically you can turn the data into long format and then use fill
. 基本上,您可以将数据转换为长格式,然后使用
fill
。
Direction down
is then equivalent to right
and direction up
equivalent to left if you use the code above. 如果使用上面的代码,则
down
方向等同于right
方向, up
方向等同于左方向。
Output: 输出:
a b c d
1 1 row1 9 13
2 2 2 10 14
3 3 row3 row3 15
4 4 4 12 16
Here the attributes are dropped, and you'd need to re-establish the type of column you want. 在这里属性被删除,您需要重新建立所需的列的类型。
This solution transposes the data frame, fills down, then transposes back again. 该解决方案转置数据帧,填充,然后再次转回。 The transposition converts the data frame to a matrix, so requires that it is converted back.
换位将数据帧转换为矩阵,因此需要将其转换回。
df <- data.frame(a = 1:4,
b = c('row1', 'row2', 'row3', 'row4'),
c = paste(9:12),
d = paste(13:16))
# remove a few values
df[2,2] <- df[3,3] <- df[4,2] <- NA
library(tidyverse)
t(df) %>%
as.data.frame %>%
fill(names(.),.direction = "down") %>%
t %>%
as.data.frame
#> a b c d
#> V1 1 row1 9 13
#> V2 2 2 10 14
#> V3 3 row3 row3 15
#> V4 4 4 12 16
Created on 2019-02-08 by the reprex package (v0.2.1.9000) 由reprex软件包 (v0.2.1.9000)创建于2019-02-08
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.