[英]Issue with na.rm = TRUE when combining multiple character columns using Unite from dplyr
When trying to combine multiple character columns using unite from dplyr, the na.rm = TRUE
option does not remove NA.当尝试使用来自 dplyr 的 unite 组合多个字符列时,
na.rm = TRUE
选项不会删除 NA。
Step by step:一步步:
word1:word5
Image of the original dataword1:word5
原始数据的图像word1:word5
in a single column using code:word1:word5
到一个列中: data_unite_5 <- data_original_5 %>%
unite("pentawords", word1:word5, sep=" ", na.rm=TRUE, remove=FALSE)
mutate_if(is.factor, as.character)
but that did not work.mutate_if(is.factor, as.character)
但这没有用。 Any suggestions would be appreciated.任何建议,将不胜感激。
You have misinterpreted how the na.rm
argument works for unite
.您误解了
na.rm
参数如何适用于unite
。 Following the examples on the tidyverse page here , z
is the unite
of x
and y
.按照此处tidyverse 页面上的示例,
z
是x
和y
的unite
。
With na.rm = FALSE
使用
na.rm = FALSE
#> z x y
#> <chr> <chr> <chr>
#> 1 a_b a b
#> 2 a_NA a NA
#> 3 NA_b NA b
#> 4 NA_NA NA NA
With na.rm = TRUE
使用
na.rm = TRUE
#> z x y
#> <chr> <chr> <chr>
#> 1 "a_b" a b
#> 2 "a" a NA
#> 3 "b" NA b
#> 4 "" NA NA
Hence na.rm
determines how NA
values appear in the assembled strings ( pentrawords
) it does not drop rows from the data.因此
na.rm
确定NA
值如何出现在组装的字符串 ( pentrawords
) 中,它不会从数据中删除行。
If you were wanting to remove the fourth row of the dataset, I would recommend filter
.如果您想删除数据集的第四行,我会推荐
filter
。
data_unite_5 <- data_original_5 %>%
unite("pentawords", word1:word5, sep =" " , na.rm = TRUE, remove = FALSE) %>%
filter(pentawords != "")
Which will exclude from your output all empty strings.这将从您的 output 中排除所有空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.