Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R计算每列缺少值的百分比_R - Fatal编程技术网

使用R计算每列缺少值的百分比

使用R计算每列缺少值的百分比,r,R,我正在使用一个大约有1000列(变量)和64000行的数据帧。我需要知道每一列缺失值的百分比,以及整个数据帧缺失值的总百分比 有人知道使用R更有效的方法吗 非常感谢 一种方法是使用tidyr::gather将数据帧转换为长格式,然后在分组后对每列应用类似的计算 假设一个数据帧(出于说明目的,小于您的数据帧): 一种方法是使用tidyr::gather将数据帧转换为长格式,然后在分组后对每列应用类似的计算 假设一个数据帧(出于说明目的,小于您的数据帧): 欢迎来到堆栈溢出!读了这篇文章,你会得到更

我正在使用一个大约有1000列(变量)和64000行的数据帧。我需要知道每一列缺失值的百分比,以及整个数据帧缺失值的总百分比

有人知道使用R更有效的方法吗


非常感谢

一种方法是使用
tidyr::gather
将数据帧转换为长格式,然后在分组后对每列应用类似的计算

假设一个数据帧(出于说明目的,小于您的数据帧):


一种方法是使用
tidyr::gather
将数据帧转换为长格式,然后在分组后对每列应用类似的计算

假设一个数据帧(出于说明目的,小于您的数据帧):


欢迎来到堆栈溢出!读了这篇文章,你会得到更富有成效的答案,尽管上面的评论让你一路顺风。您可能需要检查VIM包以查看和检查丢失的数据。非常感谢,phiver!欢迎来到堆栈溢出!读了这篇文章,你会得到更富有成效的答案,尽管上面的评论让你一路顺风。您可能需要检查VIM包以查看和检查丢失的数据。非常感谢,phiver!嗨,乔恩!非常感谢您伟大的解决方案。它按我预期的方式工作。嗨,乔恩!非常感谢您伟大的解决方案。它按我预期的方式工作。
library(tidyverse)
df <- tibble(
column = rep(paste0("col_", str_pad(1:1000, 4, pad = "0")), each = 640),
value = sample(c(0:100, NA_integer_), replace = TRUE, 6.4E5),
line = rep(1:640, 1E3)
) %>% spread(column, value)
df %>%
gather(col, value, -line) %>%
group_by(col) %>%
summarize(missing_share = mean(is.na(value)))

    # A tibble: 1,000 x 2
   col      missing_share
   <chr>            <dbl>
 1 col_0001       0.0109 
 2 col_0002       0.0141 
 3 col_0003       0.0125 
 4 col_0004       0.00938
 5 col_0005       0.0141 
 6 col_0006       0.00625
 7 col_0007       0.00312
 8 col_0008       0.00781
 9 col_0009       0.00781
10 col_0010       0.00781
# ... with 990 more rows
df_NA_overall <- df %>%
gather(col, value, -line) %>%
summarize(missing_share = mean(is.na(value)))

# A tibble: 1 x 1
  missing_share
          <dbl>
1       0.00989
map(df, ~mean(is.na(.))) 

$line
[1] 0

$col_0001
[1] 0.0109375

$col_0002
[1] 0.0140625

$col_0003
[1] 0.0125

$col_0004
[1] 0.009375

$col_0005
[1] 0.0140625

$col_0006
[1] 0.00625

$col_0007
[1] 0.003125

$col_0008
[1] 0.0078125

$col_0009
[1] 0.0078125