在 R 的两个变量中查找 NA
Finding NA in both variables in R
我想通过 count()
函数在两个长度相同的向量中同时找到 NA 值的数量:
library(tidyverse)
list1 <-c(NA,NA,3)
list2 <-c(NA,3,4)
count(is.na(list1) & is.na(list2)) # wanna get TRUE 1 FALSE 2 as one only string contains NA values in both variables
没用。存在以下错误:
Error in UseMethod("groups") :
no applicable method for 'groups' applied to an object of class "logical"
但是,在学习一本相当不错的书之前,我做到了。
library(nycflights13) #data-set
flights %>%
count(is.na(arr_delay) & is.na(dep_delay))
这里有效。似乎有些问题是从某种类型的向量转换为逻辑向量(T 或 F),但我无法弄清楚到底是什么。
您可以使用 table()
,它在 is.na(list1) & is.na(list2)
:
中的每个逻辑值处构建计数的偶然性 table
table(is.na(list1) & is.na(list2))
# FALSE TRUE
# 2 1
看起来 plyr
和 dplyr
都有 count()
功能。 plyr
版本基本上说它只是 as.data.frame(table(x))
的包装器,而 dplyr
看起来它期望 tbl()
作为输入。看起来 dplyr::count()
就是你上面的 运行。
我会在这里使用 table()
,或者显式调用 plyr::count()
:
library(tidyverse)
list1 <-c(NA,NA,3)
list2 <-c(NA,3,4)
as.data.frame(table(is.na(list1) & is.na(list2)))
#> Var1 Freq
#> 1 FALSE 2
#> 2 TRUE 1
plyr::count(is.na(list1) & is.na(list2))
#> x freq
#> 1 FALSE 2
#> 2 TRUE 1
dplyr::count(is.na(list1) & is.na(list2))
#> Error in UseMethod("groups"): no applicable method for 'groups' applied to an object of class "logical"
由 reprex package (v0.2.1)
于 2019-02-10 创建
我想通过 count()
函数在两个长度相同的向量中同时找到 NA 值的数量:
library(tidyverse)
list1 <-c(NA,NA,3)
list2 <-c(NA,3,4)
count(is.na(list1) & is.na(list2)) # wanna get TRUE 1 FALSE 2 as one only string contains NA values in both variables
没用。存在以下错误:
Error in UseMethod("groups") :
no applicable method for 'groups' applied to an object of class "logical"
但是,在学习一本相当不错的书之前,我做到了。
library(nycflights13) #data-set
flights %>%
count(is.na(arr_delay) & is.na(dep_delay))
这里有效。似乎有些问题是从某种类型的向量转换为逻辑向量(T 或 F),但我无法弄清楚到底是什么。
您可以使用 table()
,它在 is.na(list1) & is.na(list2)
:
table(is.na(list1) & is.na(list2))
# FALSE TRUE
# 2 1
看起来 plyr
和 dplyr
都有 count()
功能。 plyr
版本基本上说它只是 as.data.frame(table(x))
的包装器,而 dplyr
看起来它期望 tbl()
作为输入。看起来 dplyr::count()
就是你上面的 运行。
我会在这里使用 table()
,或者显式调用 plyr::count()
:
library(tidyverse)
list1 <-c(NA,NA,3)
list2 <-c(NA,3,4)
as.data.frame(table(is.na(list1) & is.na(list2)))
#> Var1 Freq
#> 1 FALSE 2
#> 2 TRUE 1
plyr::count(is.na(list1) & is.na(list2))
#> x freq
#> 1 FALSE 2
#> 2 TRUE 1
dplyr::count(is.na(list1) & is.na(list2))
#> Error in UseMethod("groups"): no applicable method for 'groups' applied to an object of class "logical"
由 reprex package (v0.2.1)
于 2019-02-10 创建