gpt4 book ai didi

r - 合并2个数据框与R中相同但不同的案例列

转载 作者:行者123 更新时间:2023-12-04 03:05:20 26 4
gpt4 key购买 nike

我有两个数据框,但问题是合并“by”列在不同情况下具有值。
sn1capx1e0001 与 SN1CAPX1E0001。

authors <- data.frame(
surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
nationality = c("US", "Australia", "US", "UK", "Australia"),
deceased = c("yes", rep("no", 4)))

books <- data.frame(
name = I(c("tukey", "venables", "tierney",
"tipley", "ripley", "McNeil", "R Core")),
title = c("Exploratory Data Analysis",
"Modern Applied Statistics ...",
"LISP-STAT",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
other.author = c(NA, "Ripley", NA, NA, NA, NA,
"Venables & Smith"))
m1 <- merge(authors, books, by.x = "surname", by.y = "name")

surname nationality deceased title other.author

McNeil Australia no Interactive Data Analysis NA


所以我想通过不区分大小写来合并它们。我无法使用合并或加入。
我看到我们可以使用正则表达式来匹配使用循环的值。

最佳答案

为什么不转换它们,使它们具有相同的形式?

library(stringr)

authors <- data.frame(
surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
nationality = c("US", "Australia", "US", "UK", "Australia"),
deceased = c("yes", rep("no", 4)))

books <- data.frame(
name = I(c("tukey", "venables", "tierney",
"tipley", "ripley", "McNeil", "R Core")),
title = c("Exploratory Data Analysis",
"Modern Applied Statistics ...",
"LISP-STAT",
"Spatial Statistics", "Stochastic Simulation",
"Interactive Data Analysis",
"An Introduction to R"),
other.author = c(NA, "Ripley", NA, NA, NA, NA,
"Venables & Smith"))

authors$surname <- str_to_title(authors$surname)
books$name <- str_to_title(books$name)

m1 <- merge(authors, books, by.x = "surname", by.y = "name")


   surname nationality deceased                         title other.author
1 Mcneil Australia no Interactive Data Analysis <NA>
2 Ripley UK no Stochastic Simulation <NA>
3 Tierney US no LISP-STAT <NA>
4 Tukey US yes Exploratory Data Analysis <NA>
5 Venables Australia no Modern Applied Statistics ... Ripley

关于r - 合并2个数据框与R中相同但不同的案例列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45101467/

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