[英]R - split data frame without removing NA values
If I have a df: 如果我有df:
letter body_part
a head
b head
c NA
d NA
e left_foot
And I want to split it into 2 dfs... One with only body_part - "head" and the other with everything else. 我想将其拆分为2个dfs ...一个仅包含body_part-“ head”,另一个仅包含其他内容。 Ie
即
list <- split(df, df$body_part == 'head')
列表<-split(df,df $ body_part =='head')
Can I do that without dropping the NA rows? 我可以在不删除NA行的情况下做到这一点吗? (I know I can do it if I fill the NAs with a string, but is there a way that avoids that step?)
(我知道如果我用一个字符串填充NA,我可以做到这一点,但是有办法避免这一步吗?)
From ?`%in%`
: 来自
?`%in%`
:
That '%in%' never returns 'NA' makes it particularly useful in 'if' conditions.
'%in%'从不返回'NA'使其在'if'条件下特别有用。
# just to show how the `==` comparison compares
> df$s_col <- df$body_part == 'head'
> split(df, df$body_part %in% 'head')
$`FALSE`
letter body_part s_col
3 c <NA> NA
4 d <NA> NA
5 e left_foot FALSE
$`TRUE`
letter body_part s_col
1 a head TRUE
2 b head TRUE
> ind <- df$body_part == 'head'
> ind[is.na(ind)] <- FALSE
> split(df, ind)
$`FALSE`
# A tibble: 3 x 2
letter body_part
<chr> <chr>
1 c <NA>
2 d <NA>
3 e left_foot
$`TRUE`
# A tibble: 2 x 2
letter body_part
<chr> <chr>
1 a head
2 b head
You can convert the f
argument of split()
to factor while not exluding the NA
values. 您可以将
split()
的f
参数转换为factor,而不用排除NA
值。
df <- read.table(h= T, strin = F, text = "
letter body_part
a head
b head
c NA
d NA
e left_foot")
split(df, factor(df$body_part,exclude = NULL))
#> $head
#> letter body_part
#> 1 a head
#> 2 b head
#>
#> $left_foot
#> letter body_part
#> 5 e left_foot
#>
#> $<NA>
#> letter body_part
#> 3 c <NA>
#> 4 d <NA>
split(df, factor(df$body_part,exclude = NULL) == 'head')
#> $`FALSE`
#> letter body_part
#> 3 c <NA>
#> 4 d <NA>
#> 5 e left_foot
#>
#> $`TRUE`
#> letter body_part
#> 1 a head
#> 2 b head
Created on 2019-10-14 by the reprex package (v0.3.0) 由reprex软件包 (v0.3.0)创建于2019-10-14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.