[英]Filling a column in a dataframe based on a column in another dataframe in r
我有一個評論數據框,看起來像這樣(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
我有另一個merchent名稱的數據框,看起來像這樣(df2):
Merchant_Name
Google
Android
Geoni
Visa
Apple
MC
WallMart
如果df2中的merchant_name出現在df 1的Comment中,則將該商家名稱附加到R中df1中的第二列。匹配不必是完全匹配。近似值是必需的。此外,df1包含大約500K行! 我的最終輸出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
的工作。 退房grepl
內部命令lapply
。
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
試試這個
final_df <- data.frame(Comments = character(), Merchant_Name = character(), stringsAsFactors = F)
for(i in df1$Comments){
for(j in df2$Merchant_Name){
if(grepl(tolower(j),tolower(i))){
final_df[nrow(final_df) + 1,] <- c(i, j)
break
}
}
}
final_df
## comments brands
##1 Apple laptops are really good for work,we should buy them Apple
##2 Apple Iphones are too costly,we can resort to some other brands Apple
##3 Google search is the best search engine Google
##4 Android phones are great these days Android
##5 I lost my visa card today Visa
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.