当键有空格时,r-left_join生成NA



我从左联接中得到了一个意外的NA模式。数据来自本周的"潮周二"。

library(tidyverse)
breed_traits <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_traits.csv') %>%
select(Breed, `Affectionate With Family`)
# A tibble: 195 × 2
Breed                         `Affectionate With Family`
<chr>                                              <dbl>
1 Retrievers (Labrador)                                  5
2 French Bulldogs                                        5
3 German Shepherd Dogs                                   5
4 Retrievers (Golden)                                    5
5 Bulldogs                                               4
6 Poodles                                                5
7 Beagles                                                3
8 Rottweilers                                            5
9 Pointers (German Shorthaired)                          5
10 Dachshunds                                             5     
breed_rank_all <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_rank.csv') %>%
select(Breed, `Rank 2013`)
# A tibble: 195 × 2
Breed                         `2013 Rank`
<chr>                               <dbl>
1 Retrievers (Labrador)                   1
2 French Bulldogs                        11
3 German Shepherd Dogs                    2
4 Retrievers (Golden)                     3
5 Bulldogs                                5
6 Poodles                                 8
7 Beagles                                 4
8 Rottweilers                             9
9 Pointers (German Shorthaired)          13
10 Dachshunds                             10  

Breed具有空格的行(例如,Retrievers (Labrador)(导致NA,尽管两个表中都有数据:

breed_rank_all %>%
left_join(breed_traits, by = "Breed")
# A tibble: 195 × 3
Breed                         `2013 Rank` `Affectionate With Family`
<chr>                               <dbl>                      <dbl>
1 Retrievers (Labrador)                   1                         NA
2 French Bulldogs                        11                         NA
3 German Shepherd Dogs                    2                         NA
4 Retrievers (Golden)                     3                         NA
5 Bulldogs                                5                          4
6 Poodles                                 8                          5
7 Beagles                                 4                          3
8 Rottweilers                             9                          5
9 Pointers (German Shorthaired)          13                         NA
10 Dachshunds                             10                          5

我检查了额外的空格,但不是这样。我还尝试删除Breed中的空格,然后加入,但没有。

这里有些东西不等价:

breed_rank_all$Breed[1]
[1] "Retrievers (Labrador)"
breed_traits$Breed[1]
[1] "Retrievers (Labrador)"
breed_rank_all$Breed[1] == breed_traits$Breed[1]
[1] FALSE

更新

区别是c2 a0代替了20??

iconv(breed_rank_all$Breed[1], toRaw = TRUE)
[[1]]
[1] 52 65 74 72 69 65 76 65 72 73 20 28 4c 61 62 72 61 64 6f 72 29
> iconv(breed_traits$Breed[1], toRaw = TRUE)
[[1]]
[1] 52 65 74 72 69 65 76 65 72 73 c2 a0 28 4c 61 62 72 61 64 6f 72 29

使用stringi::stri_enc_toascii

> stringi::stri_enc_toascii(breed_traits$Breed[1])
[1] "Retrievers32(Labrador)"
> stringi::stri_enc_toascii(breed_rank_all$Breed[1])
[1] "Retrievers (Labrador)"

这似乎解决了问题:

breed_traits <- breed_traits %>%
mutate(Breed = stringi::stri_enc_toascii(Breed),
Breed = gsub("\32", " ", Breed)) 

我发现了这个问题。我凭直觉调查了一下空白处。

# space that isn't a space (like non-breaking space?)
utf8::utf8_print(breed_traits$Breed[1], utf8 = FALSE)
# [1] "Retrieversu00a0(Labrador)"
# this is a non-breaking space

您可以用正则表达式替换不间断空格。

(replSp = str_replace_all(string = breed_traits$Breed[1],
pattern = "[[:space:]]",
replacement = " ")) 
# [1] "Retrievers (Labrador)" 
breed_rank_all$Breed[[1]] == replSp
# [1] TRUE 


根据要求。。。

要替换数据帧中的所有非中断空间:

breed_traits <- breed_traits %>% 
mutate(Breed = str_replace_all(string = Breed, 
pattern = "[[:space:]]", 
replacement = " "))

最新更新