当两个数据都有共同的列时,如何使用查找数据帧中的值替换主数据帧中的值?

时间:2019-05-10 17:32:35

标签: r string

我有两个带有dput的数据帧,如下所示:

#main data frame
mydf <- structure(list(words = c("banna", "pocorn and drnk", "trael", 
                                   "rabbitt", "emptey", "ebay", "templete", "interne", "bing", "methog", 
                                   "tullius"), words_checking = list("banna", c("pocorn", "drnk"
                                   ), "trael", "rabbitt", "emptey", "ebay", "templete", "interne", 
                                   character(0), "methog", "tullius"), row_num = c("1", "2", 
                                                                                   "3", "4", "5", "6", "7", "8", "9", "10", "11")), row.names = c(NA, 
                                                                                                                                                  -11L), class = "data.frame")

#look up data frame
mydf_1 <- structure(list(words = c("banna", "pocorn and drnk", "pocorn and drnk", "trael", "rabbitt", "emptey", "ebay", "templete", "interne", "methog", "tullius"), row_num = c("1", "2", "2", "3", "4", "5", "6", "7", "8", "10", "11"), words_checking = c("banna", "pocorn", "drnk", "trael", "rabbitt", "emptey", "ebay", "templete", "interne", "methog", "tullius"), suggestion = list(c("banns", "banana", "manna", "wanna", "Hanna", "bandanna", "banal", "ban"), c("popcorn", "porn", "corn"), c("drink", "drank", "drunk", "dink", "dank", "dunk"), c("trawl", "travel", "trail", "trammel"), c("rabbit", "rabbits", "Babbitt", "rabbit t", "rabbi", "terabit"), c("empty", "tempt", "temp"), c("eBay", "bay", "e bay"), c("template", "temple", "complete", "platelet"), c("intern", "internee", "internet", "interned", "interns", "intern e", "interline", "intervene", "intertwine", "interment"), c("method", "met hog", "met-hog", "methought"), "tulips"), first_suggestion = list("banns", "popcorn", "drink", "trawl", "rabbit", "empty", "eBay", "template", "intern", "method", "tulips"), second_suggestion = list("banana", "porn", "drank", "travel", "rabbits", "tempt", "bay", "temple", "internee", "met hog", NA_character_), third_suggestion = list("manna", "corn", "drunk", "trail", "Babbitt", "temp", "e bay", "complete", "internet", "met-hog", NA_character_), fourth_suggestion = list("wanna", NA_character_, "dink", "trammel", "rabbit t", NA_character_, NA_character_, "platelet", "interned", "methought", NA_character_)), row.names = c(NA, -11L), class = "data.frame")

我想使用mydf_1$first_suggestion列并替换words中拼写错误的mydf列。我相信我可以利用row_num列来做到这一点,但不确定从哪里开始。有此经验的人可以提供建议吗?

这是我对mydf的期望输出

mydf_desired <- structure(list(words = c("banna", "pocorn and drnk", "trael", 
                                         "rabbitt", "emptey", "ebay", "templete", "interne", "bing", "methog", "tullius"), 
                               words_checking = list("banna", c("pocorn", "drnk"), "trael", "rabbitt", "emptey", "ebay", "templete", "interne", character(0), "methog", "tullius"), 
                               row_num = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"),
                               words_first_suggestion = c("banns", "popcorn and drink", "trawl", "rabbit", "empty", "eBay", "template", "intern", "bing", "method", "tulips")), 
                          row.names = c(NA, -11L), class = "data.frame")

0 个答案:

没有答案