r语言 - 当行不消失时删除 data.frame 中的所有空列和行



我有两个数据帧,dA(这里(和dB(这里(。它们完全相同,除了dB有一个完全空的列和多个空行。

dA <- read.csv("https://raw.githubusercontent.com/izeh/m/master/irr3.csv", h = T)
dB <- read.csv("https://raw.githubusercontent.com/izeh/m/master/irr4.csv", h = T)

我想删除dB中的所有空列和所有空行,以便dB变得完全像dA.

目前,我尝试以下方法来实现我的目标,但看起来空行没有被删除

# remove columns with all NA
B1 <- dB[, colSums(is.na(dB)) != nrow(dB)]
# remove rows with all NA
B2 <- B1[rowSums(is.na(B1)) != ncol(B1), ]   # NOW, check by:  `nrow(B2)` the `NA` haven't 
# been removed !!

您有NA行和空行。你可以做

B1[rowSums(is.na(B1) | B1 == "") != ncol(B1), ]
#   study.name  group.name outcome ESL prof scope type
#1  Shin.Ellis    ME.short       1   1    2     1    1
#2  Shin.Ellis     ME.long       1   1    2     1    1
#3  Shin.Ellis   DCF.short       1   1    2     1    2
#4  Shin.Ellis    DCF.long       1   1    2     1    2
#5  Shin.Ellis  Cont.short       1   1    2    NA   NA
#6  Shin.Ellis   Cont.long       1   1    2    NA   NA
#8    Trus.Hsu       Exper       1   2    2     2    1
#.....

我们也可以使用filter_alldplyr

library(dplyr)
B1 %>% filter_all(any_vars(!is.na(.) & . != ""))

这是一个选项,其中包含来自base RFilter

Filter(function(x) !all(is.na(x)), dB)
#  study.name  group.name outcome ESL prof scope type
#1  Shin.Ellis    ME.short       1   1    2     1    1
#2  Shin.Ellis     ME.long       1   1    2     1    1
#3  Shin.Ellis   DCF.short       1   1    2     1    2
#4  Shin.Ellis    DCF.long       1   1    2     1    2
#5  Shin.Ellis  Cont.short       1   1    2    NA   NA
#6  Shin.Ellis   Cont.long       1   1    2    NA   NA
#7                              NA  NA   NA    NA   NA
#8    Trus.Hsu       Exper       1   2    2     2    1
#...

或与any

Filter(function(x) any(!is.na(x)), dB)

删除行

B1[!!rowSums(!is.na(B1) & B1 != ""),] 

或使用Reduce

B1[Reduce(`|`, lapply(B1, function(x) !is.na(x) & x != "" )),]

您可以使用replace""转换为NA并使用然后使用is.na。要删除完整的行和列,您可以将apply与函数all一起使用。

x  <- is.na(replace(dB, dB=="", NA))
dB[!apply(x, 1, all), !apply(x, 2, all)]

或使用rowSumscolSums,如问题中:

x <- is.na(dB) | dB == ""
dB[rowSums(x) != ncol(x), colSums(x) != nrow(x)]

最新更新