R:删除行但跳过 NA
R: Deleting rows but skip NA
使用此代码,我假装删除了 a
列中包含单词 "TRUE"
的行。
DATA2 <- DATA[!DATA$a == "TRUE”]
但是,我有 "TRUE"
、"FALSE"
和 "NA"
。当我 运行 时,此代码 R 也会删除 NA。我怎样才能跳过这个并只删除带有 "TRUE"?
的行
我已经尝试过这个,但没有成功...
DATA2 <- DATA[!DATA$a=='TRUE',na.rm= FALSE]
错误:
Error in `[.data.frame`(DATA, !DATA$a == "TRUE", :
unused argument (na.rm = FALSE)
首先创建一些示例数据:
set.seed(1)
df = data.frame(x = runif(10),
y = runif(10),
z = sample(c('TRUE', 'FALSE', NA), 10, replace = TRUE),
stringsAsFactors = FALSE) # Force to character, and not factor
我在这里使用的技巧是将过滤器中的 NA
替换为 "FALSE"
:
df[!ifelse(is.na(df$z), 'FALSE', df$z) == 'TRUE',]
x y z
1 0.26550866 0.2059746 <NA>
3 0.57285336 0.6870228 FALSE
6 0.89838968 0.4976992 FALSE
8 0.66079779 0.9919061 FALSE
9 0.62911404 0.3800352 <NA>
10 0.06178627 0.7774452 FALSE
我非常喜欢 dplyr
编程风格:
df %>% filter(ifelse(is.na(z), 'FALSE', z) != 'TRUE')
x y z
1 0.26550866 0.2059746 <NA>
2 0.57285336 0.6870228 FALSE
3 0.89838968 0.4976992 FALSE
4 0.66079779 0.9919061 FALSE
5 0.62911404 0.3800352 <NA>
6 0.06178627 0.7774452 FALSE
我创建了一些可重现的数据:
df <- data.frame(
col1 = c(1:15),
col2=rep(c("TRUE","FALSE", "NA"),5),
stringsAsFactors = FALSE)
使用 base R,你可以这样做:
df2 <- df[df$col2 == "NA" | !df$col2 == "TRUE", ]
在 dplyr 中:
library(dplyr)
df2 <- df %>% filter(col2 == "NA" | !col2 == "TRUE" )
输出:
> df2
col1 col2
2 2 FALSE
3 3 NA
5 5 FALSE
6 6 NA
8 8 FALSE
9 9 NA
11 11 FALSE
12 12 NA
14 14 FALSE
15 15 NA
// 编辑:
将 NA
值更改为问题中提供的字符串 ("NA"
)。
// 注:
如果你想把"TRUE"
转换成TRUE
,"FALSE"
转换成FALSE
,"NA"
转换成NA
,你可以这样做:
df_bool <- data.frame(
col1 = df$col1,
col2 = as.logical(df$col2)
)
因为 df_bool$col2
将 return 真正的逻辑值而不是看起来像逻辑值的字符串,它可以在 if
本身内使用,而不必使用 ==
TRUE
和 FALSE
值:
df2 <- df_bool[!df_bool$col2 | is.na(df_bool$col2), ]
使用此代码,我假装删除了 a
列中包含单词 "TRUE"
的行。
DATA2 <- DATA[!DATA$a == "TRUE”]
但是,我有 "TRUE"
、"FALSE"
和 "NA"
。当我 运行 时,此代码 R 也会删除 NA。我怎样才能跳过这个并只删除带有 "TRUE"?
我已经尝试过这个,但没有成功...
DATA2 <- DATA[!DATA$a=='TRUE',na.rm= FALSE]
错误:
Error in `[.data.frame`(DATA, !DATA$a == "TRUE", :
unused argument (na.rm = FALSE)
首先创建一些示例数据:
set.seed(1)
df = data.frame(x = runif(10),
y = runif(10),
z = sample(c('TRUE', 'FALSE', NA), 10, replace = TRUE),
stringsAsFactors = FALSE) # Force to character, and not factor
我在这里使用的技巧是将过滤器中的 NA
替换为 "FALSE"
:
df[!ifelse(is.na(df$z), 'FALSE', df$z) == 'TRUE',]
x y z
1 0.26550866 0.2059746 <NA>
3 0.57285336 0.6870228 FALSE
6 0.89838968 0.4976992 FALSE
8 0.66079779 0.9919061 FALSE
9 0.62911404 0.3800352 <NA>
10 0.06178627 0.7774452 FALSE
我非常喜欢 dplyr
编程风格:
df %>% filter(ifelse(is.na(z), 'FALSE', z) != 'TRUE')
x y z
1 0.26550866 0.2059746 <NA>
2 0.57285336 0.6870228 FALSE
3 0.89838968 0.4976992 FALSE
4 0.66079779 0.9919061 FALSE
5 0.62911404 0.3800352 <NA>
6 0.06178627 0.7774452 FALSE
我创建了一些可重现的数据:
df <- data.frame(
col1 = c(1:15),
col2=rep(c("TRUE","FALSE", "NA"),5),
stringsAsFactors = FALSE)
使用 base R,你可以这样做:
df2 <- df[df$col2 == "NA" | !df$col2 == "TRUE", ]
在 dplyr 中:
library(dplyr)
df2 <- df %>% filter(col2 == "NA" | !col2 == "TRUE" )
输出:
> df2
col1 col2
2 2 FALSE
3 3 NA
5 5 FALSE
6 6 NA
8 8 FALSE
9 9 NA
11 11 FALSE
12 12 NA
14 14 FALSE
15 15 NA
// 编辑:
将 NA
值更改为问题中提供的字符串 ("NA"
)。
// 注:
如果你想把"TRUE"
转换成TRUE
,"FALSE"
转换成FALSE
,"NA"
转换成NA
,你可以这样做:
df_bool <- data.frame(
col1 = df$col1,
col2 = as.logical(df$col2)
)
因为 df_bool$col2
将 return 真正的逻辑值而不是看起来像逻辑值的字符串,它可以在 if
本身内使用,而不必使用 ==
TRUE
和 FALSE
值:
df2 <- df_bool[!df_bool$col2 | is.na(df_bool$col2), ]