簡體   English   中英

在R中合並具有相同但不同大小寫列的2個數據框

[英]merge 2 dataframe with same but different case column in R

我有兩個數據框,但問題是合並“ 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")

姓死者頭銜其他

McNeil Australia沒有交互式數據分析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

我發現這很簡單

都使用“ toupper()”進行隱蔽

books$name<-toupper(books$name) 

簡單....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM