gpt4 book ai didi

r - 根据 r 中另一个数据框中的列填充数据框中的列

转载 作者:行者123 更新时间:2023-12-01 11:29:42 27 4
gpt4 key购买 nike

我有一个评论数据框,看起来像这样(df1)

Comments
Apple laptops are really good for work,we should buy them
Apple Iphones are too costly,we can resort to some other brands
Google search is the best search engine
Android phones are great these days
I lost my visa card today

我有另一个商家名称数据框,看起来像这样(df2):

Merchant_Name
Google
Android
Geoni
Visa
Apple
MC
WallMart

如果 df2 中的 merchant_name 出现在 df 1 的 Comment 中,将该商家名称附加到 R 中 df1 的第二列。匹配不需要完全匹配。需要的是近似值。此外,df1 包含大约 500K 行!我的最终 ooutput df 可能看起来像这样

Comments                                                        Merchant
Apple laptops are really good for work,we should buy them Apple
Apple Iphones are too costly,we can resort to some other brands Apple
Google search is the best search engine Google
Android phones are great these days Android
I lost my visa card today Visa

我怎样才能在 R 中高效地做到这一点??谢谢

最佳答案

这是regex 的工作。查看 lapply 中的 grepl 命令。

comments = c(
'Apple laptops are really good for work,we should buy them',
'Apple Iphones are too costly,we can resort to some other brands',
'Google search is the best search engine ',
'Android phones are great these days',
'I lost my visa card today'
)

brands = c(
'Google',
'Android',
'Geoni',
'Visa',
'Apple',
'MC',
'WallMart'
)

brandinpattern = lapply(
brands,
function(brand) {
commentswithbrand = grepl(x = tolower(comments), pattern = tolower(brand))
if ( sum(commentswithbrand) > 0) {
data.frame(
comment = comments[commentswithbrand],
brand = brand
)
} else {
data.frame()
}
}
)

brandinpattern = do.call(rbind, brandinpattern)


> do.call(rbind, brandinpattern)
comment brand
1 Google search is the best search engine Google
2 Android phones are great these days Android
3 I lost my visa card today Visa
4 Apple laptops are really good for work,we should buy them Apple
5 Apple Iphones are too costly,we can resort to some other brands Apple

关于r - 根据 r 中另一个数据框中的列填充数据框中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688413/

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