gpt4 book ai didi

r - left_join 在键有空格时产生 NA

转载 作者:行者123 更新时间:2023-12-05 03:29:41 24 4
gpt4 key购买 nike

我从左连接中得到了意想不到的 NA 模式。数据来自this week's Tidy Tuesday .

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] "Retrievers\032(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("\\\032", " ", Breed))

最佳答案

我发现了问题。凭直觉,我调查了空​​白。

# space that isn't a space (like non-breaking space?)
utf8::utf8_print(breed_traits$Breed[1], utf8 = FALSE)
# [1] "Retrievers\u00a0(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 = " "))

关于r - left_join 在键有空格时产生 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70980101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com