【问题标题】:Match text names in R data.table匹配 R data.table 中的文本名称
【发布时间】:2018-01-06 23:57:30
【问题描述】:

我想将不同的 data.tables 与艺术家数据合并。但是,在我的某些数据集中,艺术家姓名的拼写方式有所不同。我正在寻找一种简单方便的方法来匹配这些艺术家姓名,以便我为每个艺术家提供一个 ID,这样可以更轻松地合并我的数据集。我对 R 还是很陌生,我想知道您是否可以就这个主题给我一些指导。艺术家姓名基本上是不同data.tables中的字符串。

【问题讨论】:

  • 可重现的例子?

标签: r data.table


【解决方案1】:

您可以使用library(plyr) 中的?revalue 来更正名称,然后合并它们

dt_age <- data.table(artist=c("Dali","Van Gogh"),
                 age=c(85,37))
dt_paintings <- data.table(artist=c("dali","van gogh"),
                     paintings=c("The peristence of Memory","The Starry Night"))

merge(dt_age,dt_paintings,by="artist") # this is empty

artist_correct <- c("dali"="Dali",
                        "van gogh"="Van Gogh"
                    )
dt_paintings$artist <- revalue(dt_paintings$artist,artist_correct)

merge(dt_age,dt_paintings,by="artist") # this is correct after we correct the names

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多