如何在r / sparklyr中的数据集中提取没有空值的列名?

时间:2019-02-06 13:45:04

标签: r sparklyr

我只想提取r中大型数据集中没有空值的列名。

如果我的表有4列(id,Price,Product,Status),其中Price和Status列具有一些空值,而id和Product列则没有空值。然后,我希望输出为:id,产品

2 个答案:

答案 0 :(得分:0)

如果您需要确切的答案,则必须先扫描整个数据集,以计算缺失值:

library(dplyr)

df <- copy_to(sc, tibble(
  id = 1:4,  Price = c(NA, 3.20, NA, 42),
  Product = c("p1", "p2", "p3", "p4"),
  Status = c(NA, "foo", "bar", NA)))

null_counts <- df %>% 
    summarise_all(funs(sum(as.numeric(is.na(.)), na.rm=TRUE))) %>% 
    collect() 

null_counts
# A tibble: 1 x 4
     id Price Product Status
  <dbl> <dbl>   <dbl>  <dbl>
1     0     2       0      2

确定哪些列的缺失计数等于零:

cols_without_nulls <- null_counts %>% 
  select_if(funs(. == 0)) %>% 
  colnames()

cols_without_nulls
[1] "id"      "Product"

并使用它们进行选择

df %>% select(one_of(cols_without_nulls))
# Source: spark<?> [?? x 2]
     id Product
  <int> <chr>  
1     1 p1     
2     2 p2     
3     3 p3     
4     4 p4 

存在一个较短的变体:

df %>% select_if(funs(sum(as.numeric(is.na(.)), na.rm=TRUE) == 0))
Applying predicate on the first 100 rows
# Source: spark<?> [?? x 2]
     id Product
  <int> <chr>  
1     1 p1     
2     2 p2     
3     3 p3     
4     4 p4    

但是如您所见,它将仅对数据进行采样。

答案 1 :(得分:-1)

data <- data.frame(ID = c(1,2,3,4),
                   Price = c(50, NA, 10, 20),
                   Product = c("A", "B", "C", "D"),
                   Status = c("Complete", NA, "Complete", "Incomplete"))

names(apply(data, 2, anyNA)[apply(data, 2, anyNA) == FALSE])