提迪尔不计算数据帧中的NA

时间:2018-06-23 09:03:43

标签: r dplyr tidyr

如何在下面的代码中不计算NA!

数据框

   CREATE OR REPLACE PROCEDURE hr.countEmployee(passin IN NUMBER,OUT_CURSOR OUT  sys_refcursor)
    IS
    BEGIN
    OPEN OUT_CURSOR FOR
    SELECT COUNT(*) FROM hr.mitch_employee_motors WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
    END;

代码:

df<-read.table(text="
Name per1 per2 per3
a1 000 xxx 230
a1 xxx 000 NA
a2 000 340 xxx
a3 000 xxx NA",
header=TRUE, stringsAsFactor = FALSE)   

问题:其他人也算不上

1 个答案:

答案 0 :(得分:1)

如果我正确理解,可以在filter之后使用NA删除gather

library(tidyverse)
library(reshape2)
df %>% 
  gather(key, value, -Name) %>%
  filter(!is.na(value)) %>% 
  mutate(value = ifelse(value %in% c("000", "xxx"), value, "Others")) %>%
  dcast(., Name ~ value, length)
#  Name 000 Others xxx
#1   a1   2      1   2
#2   a2   1      1   1
#3   a3   1      0   1

或者,如果您也想对每个“名称”计算NA,则可以使用case_when(而不是另一个ifelse)将其转换为字符串

df %>%
  gather(key, value, -Name) %>%
  mutate(value =
           case_when(
             value %in% c("000", "xxx") ~ value,
             is.na(value) ~ "NA",
             TRUE ~ "Others"
           )) %>%
  dcast(Name ~ value, fun.aggregate = length)
#  Name 000 NA Others xxx
#1   a1   2  1      1   2
#2   a2   1  0      1   1
#3   a3   1  1      0   1