简体   繁体   English

跨列填充data.frame中的缺失值

[英]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.

相关问题 在data.frame中缺少值的列之间求和 - summing across columns with missing values in a data.frame 从data.frame中提取时用NA填充缺失的列 - fill missing columns with NA while extracting from a data.frame 根据跨列的行值将data.frame拆分为列表 - split data.frame into list based on row values across columns 使用相同数据框中的数据填充data.frame中的缺失值 - Fill missing values in the data.frame with the data from the same data frame R-根据一列中跨不同列的公共值,将data.frame格式化为另一个“组合” data.frame - R- format a data.frame into another 'combined' data.frame based on common values within a column dependent across different columns 使用所有递归索引作为列名将嵌套列表转换为 data.frame,并用 NA 填充缺失的列 - Convert nested lists to data.frame using all recursive indexes as colnames and fill missing columns with NAs 在所有列中交叉列出SparkR数据框中的缺失值 - Cross tabulating missing values in SparkR data frame across all columns 使用组内完整的 dplyr 填充 data.frame 中的缺失值 - Fill missing values in data.frame using dplyr complete within groups 从data.frame中各列的每一行中随机选择值,然后在R中取平均值 - randomly select values from each row across columns in a data.frame and average them in R R:根据条件根据行值在data.frame中填充新列? - R: fill new columns in data.frame based on row values by condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM