簡體   English   中英

當僅缺少兩列時,如何刪除 NA?

[英]How can I remove NAs when both columns are missing only?

我在 R 中有一個 df 如下:

ID    Age   Score1     Score2      
2      22    12           NA
3      19    11           22
4      20    NA           NA
1      21    NA           20

現在我只想刪除缺少 Score 1 和 Score 2 的行(即第 3 行)

你可以像這樣過濾它:

df <- read.table(head=T, text="ID    Age   Score1     Score2      
2      22    12           NA
3      19    11           22
4      20    NA           NA
1      21    NA           20")
df[!(is.na(df$Score1) & is.na(df$Score2)), ]
#   ID Age Score1 Score2
# 1  2  22     12     NA
# 2  3  19     11     22
# 4  1  21     NA     20

即在沒有 ( ! ) Score1丟失和 ( & ) Score2丟失的地方取行。

一種選擇是rowSums

df1[ rowSums(is.na(df1[grep("Score", names(df1))])) < 2,]

或者使用base R另一種選擇

df1[!Reduce(`&`, lapply(df1[grep("Score", names(df1))], is.na)),]

數據

df1 <- structure(list(ID = c(2L, 3L, 4L, 1L), Age = c(22L, 19L, 20L, 
 21L), Score1 = c(12L, 11L, NA, NA), Score2 = c(NA, 22L, NA, 20L
 )), class = "data.frame", row.names = c(NA, -4L))

這是帶有dplyr兩個版本,它們可以擴展到許多帶有前綴“Score”的列。

使用filter_at

library(dplyr)

df %>% filter_at(vars(starts_with("Score")), any_vars(!is.na(.)))

#  ID Age Score1 Score2
#1  2  22     12     NA
#2  3  19     11     22
#3  1  21     NA     20

filter_if

df %>% filter_if(startsWith(names(.),"Score"), any_vars(!is.na(.)))

帶有apply基本 R 版本

df[apply(!is.na(df[startsWith(names(df),"Score")]), 1, any), ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM