首页 > 解决方案 > 如何colSum按日期分组

问题描述

我有一个大表,其中包含一个评论列(包含大量文本)和一个发布评论的日期列。我创建了一个单独的关键字向量(我们称之为键),我想计算每天有多少匹配项。这让我很接近,但是它计算了整个数据集的匹配,我需要每天对其进行分解。编码:

key <- c('A','B','C','D')

table_mentions <- data.frame(key, colsum(sapply(key, grepl, table_orig$comment)))

这将返回以下格式:

钥匙 提及次数
一种 85
52
C 54
D 54

这很接近,但它是整个数据集的匹配计数。我需要按以下格式分解数据:

通讯日期 钥匙 提及次数
03-01-21 一种 4
03-01-21 3
03-01-21 C 7
03-01-21 D 5
03-02-21 一种 6
03-02-21 3
03-02-21 C 2
03-02-21 D 6
03-03-21 一种 5
03-03-21 6
03-03-21 C 9
03-03-21 D 11

....从那里依此类推(我编造了第二张桌子的数字)...

我一生都无法弄清楚如何使这项工作。我尝试按日期拆分数据框,但我不确定如何将拆分数据框列表传递到上面的函数中,然后将结果合并在一起。

谢谢!

编辑:这是 dput(table_orig)。

structure(list(comm_date = c("01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", "01-03-21", 
"01-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", 
"02-03-21", "02-03-21", "02-03-21", "02-03-21", "02-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", "03-03-21", 
"03-03-21", "03-03-21", "03-03-21"), comment = c("I know the eyes are on BTC and", 
"Isnt it funny how for years te", "Coinbase just made a bunch of ", 
"Still no ADA though?", "No", "What color do you associate Ma", 
"... Red?", "That was February", "See ya in the next daily discu", 
"C u there bro", "Just woke up in all this green", "How long time it\031s taking for ", 
"Chill.  Check in another 10 mi", "Depends on the coin/network.  ", 
"Depends on the coin.", "Is it just me or is the Crypto", "Resistance at 50k is tough", 
"For now...", "have to wait 3 minutes for new", "Correction over? I feel like w", 
"We might start barting for a f", "Knock, knock, knockin' on Heav", 
"See you guys tomorrow for more", "Before we get to the next dail", 
"Fomo back in!!!!! Gogogogoogoo", "AAVE you beast=\200", "I remember interacting with a ", 
"Aave (AAVE), Balancer (BAL), B", "Ah, that's probably part of wh", 
"LRC is garbo", "wait... NY residents couldn't ", "Grandma Bitcoin, what mighty b", 
"The biggest ones =", "Remember this feeling next tim", "Be a bit weary during Q4 2021 ", 
"These AAVE and UNI longs got m", "Very bullish on UNI. UNI v3 sh", 
"If it can beat the 50k wall, s", "not trying to jinx it, but I\031m", 
"&amp;#x200B;\n\n![gif](giphy|Qus", "Balls deep in scrt", "SKKKRRRRRTTT", 
"More like - internet money pri", "Look at that green btc bar!", 
"50k heeeereeee we come again", "What society [think](https://i", 
"in our jammies watching our sc", "Moonsszszszszsz", "Can someone please make BTC re", 
"What is your favourite badly n", "Polkadot", "OMG and most DeFi food tokens", 
"My favourite one is SYNTHETIX ", "besmirch the name all you want", 
"SYNTHETIX sounds like a knock ", "and i love it", "https://cointelegraph.com/news", 
"Won't lie, I feel like Cosmos ", "I have never done acid but I k", 
"You\031ve got a pretty good idea", "hahah I was tempted to mention", 
"Be advised, the website cointe", "Why is barely anyone talking a", 
"Because they'll spend $60 a we", "They literally announce partne", 
"Thx for bringing it to our att", "Welcome back to tonight's epis", 
"https://i.imgur.com/O0WKuya.jp", "Shhh peeps, don't wake the 498", 
"Ugh who coughed?", "Sorry that\031s just what my fart", "Best exchange to swap ADA for ", 
"Binance, Kraken", "Binance or Kraken depending on", "I'm in Canada. Don't think Bin", 
"Binance works in Canada", "I am not Canadian but I think ", 
"Binance?", "Playing some games, drinking a", "I approve of this lifestyle", 
"Yeah screw work, although it i", "I sometimes reminisce about th", 
"Which games friend?", "Take a shot every time price r", "Set the graph to 1m candles, t", 
"My favorite game, \030get rich an", "Give me the year of release an", 
"I am so sick and tired of hear", "<µThis one\031s optimistic<¶", 
"Eth is my second biggest holdi", "bro... the reason the fees are", 
"If it makes you feel better, m", "https://i.imgur.com/Dzs6JLo.jp", 
"![gif](giphy|fw90GAvm9cVn5bMuz", "=\002=\002=\002 You win", 
"Ok that\031s a pretty good shitpo", "[deleted]", "hump hump day?  Or hump day hu", 
"Who\031s on the pancake train??", "am experimenting with cake far", 
"How is it working out for you?", "Me, love some pancakes", "Nope, too much Binance involve", 
"That the thing bro, Binance is", "Need another LTC run up", 
"Sounds weird but I feel like $", "Interestingly enough, in 2017/", 
"It's a big deal because it got", "=¨ALERT=¨ALERT=¨\nDentaCoin is ", 
"DentaCoin and WaltonCoin have ", "DentaCoin definitely knew what", 
"![gif](giphy|26FxsQwgJyU4me9ig", "Changed my portfolio to dollar", 
"Same but the other way around.", "Same but neither", "i want to take a shower, but c", 
"Modern mans dilemma", "Foolish btc bears at 49800", "Hoping for that 10 trillion ma", 
"Not gonna happen.\n\nMaybe the n", "Juicy recovery!", "Stupid how exciting it is to s", 
"We are IN this together", "It\031s nice to have a place wher", 
"Gotta be honest that a slight ", "The feelings mutual friend =I=", 
"What's the best coin to throw ", "For a 10x gain in a year , you", 
"SCRT and IOV are the ones I am", "ETH if ETH2 comes into play, o", 
"ETH", "Bitcoin Cash. Once people stop", "BTC, ETH", "S&amp;P500", 
"BoomerCoin", "lmao. He said $1K to $10K, not", "C'mon LTO, hit that $0.40 and ", 
"Grayscale just bought a massiv", "Good and green morning guys! J", 
"Your best bet is to ask on the", "Thx!", "What's your ethereum price pre", 
"7 billion gorillian dollars", "Gawdaamnnnnnn that's a lotttaa", 
"7,5k", "5,000", "NANO do something :(", "nano says nono :(", 
"Anybody else NOT buy in this m", "Many projects are still dipped", 
"Sadly, I'm only on robinhood. ", "[removed]", "Your comment was removed becau", 
"This is the best entrance you'", "I don't think that's true. Thi", 
"Welp I guess I'd be saying the", "Well, we'll see at the end of ", 
"Better entrance? ETH dipped 35", "Oh, I figured it'd get closer ", 
"get out of robinhood immediate", "Any common issues with coinbas", 
"Use coinbase pro instead of co", "Limited coin selection and hig", 
"You can buy ETH from Coinbase ", "When are the stimulus cheques ", 
"If they come late March/Early ", "NANO and ATOM - cmon_do_someth", 
"I go to dinner and red candles", "This year is gonna change peop", 
"I get paid tomorrow..so we sho", "Beautiful morning! Thank you, ", 
"See y'all on the next thread !", "Riseeeelodocusssssss", "OMI starts big marketing campa", 
"Where do coins do their market", "you can buy them on BitForex", 
"TaqoBear soon died from Alcoho", "A late good morning from Austr", 
" Murder galore @ those shorter", "LETS FUCKING GO", "We did it! 50k! <‰=-=-=-=-", 
"To all the People that bought ", "i think probably going to have", 
"I like this week more than las", "ETH ate its wheaties this morn", 
"Alexa play break on through", "Love the media, shit all over ", 
"Imagine both the FOMO and FUD ", "[deleted]", "Algo is very interesting and c", 
"Tysm! Have done as much resear", "Study them until your eyes ble", 
"Dot and Link are making me hap", "Link has been amazing today! S", 
"We thinking ETH $2k end of the", "Possible!", "Definitely! Maybe even sooner,", 
"Come on Bitcoin! You can break", "Going well beyond that", "Of course, but just trying to ", 
"We got dat 50k now babyy", "Drinking game: take a shot eve", 
"Ill start now", "bout to go sell some bags, if ", "Google takes your privacy, gov", 
"Unless your using xmr...blockc", "Anyone else having issues with", 
"Come on guys. If you own ADA b", "Thanks for the clarification, ", 
"Yes lol, for a few days alread", "You can withdraw 24/7. There a", 
"LOL. Good sir, that's just not", "Well of course during maintena", 
"During maintenance? And the po", "You are ignorant", "![gif](giphy|oTRNxpuGjznAk)", 
"member when this place was a g", "See you in another life brotha", 
"Come with me, Hail Mary  \nRun ", "Feeling quite optimistic about", 
"I\031m suddenly feeling optimisti", "Glad to see the optimism of so", 
"Those three are my biggest hol", "Amazing portfolio !!", "I'm a big fan of LINK", 
"Two wrongs make a right?", "How can Coinbase not list the ", 
"Agreed. Not sure what they hav", "Classic Coinbase", "Na feb is blue", 
"Thanks man I got it\nI fist tra", "Tnx", "Tnx I got it", "I give it 20 mins", 
"Yeah annoying. But if BTC brok", "Hay, hay, hay hay hayyy", 
"See you buddy!", "I might be wrong, but nobody c", "Since when?", 
"We can't even sign up for some", "Are you using TOR?", "I do not. I just use a VPN.  D", 
"The dip is the light if you ha", "If? Ye of little faith.\n\n52k i", 
"We never left. Only noobs got ", "Far out man.\n\nEdit: read sente", 
"Pretty much all I saw:\n\n![gif]", "Lmao. =3That\031s about right.", 
"The hunt: Showdown", "And until L2\031s or EIP 1559 are", "Exactly", 
"I\031ve been in crypto for many y", "I completely understand what y", 
"From the first sentence of my ", "This was quality my guy LOL", 
"I don't have a whole lot in bu", "Crossing 50K; the gift that ke", 
"True enough, I should not be s", "Same i opened a long near the ", 
"Approaching liquidation is qui", "Possible, but btc will still d", 
"ADA has more HODLers than trad", "Its 24 volume is a higher perc", 
"I\031d be curious what that numbe", "Cardo is a vegetable tha is al", 
"Reminds me of that episode of ", "Jajajaja, that is literally th", 
"No you\031re suppose to dip until", "Everyone laughed at me for buy", 
"Laughed? Laughing. Cause we ne", "ATH incoming", "ETH is like that girl in a mov", 
"I really should do analysis of", "You do that. If you need me, I", 
"So MIR staking APY just went u", "Totally agree. Staking has bee", 
"I dont earn any staking income", "Nano dropping out of the top 1", 
"I\031m I only retard who invested", "If we close the daily above $4", 
"Why 48.3 in particular?", "What are you looking at for sp", 
"The ema 50 and 100 are right a", "Perhaps the bitcoin is fueled ", 
"Any fresh traction?", "Does anyone have a ticker/dash", "I wrote a script to pull the t", 
"Dip into the market, don't dip", "When I dip you dip we dip\n\n&am", 
"Me too lol. Got some more ETH ", "Its strange that no-one has me", 
"ASIA LESGOOOOOOOOOOO", "Yes Asia go! Except you, F2poo", "How do i sell the Moons i have", 
"Sell them on Honeyswap. Transf", "So I\031m sure there\031s a better w", 
"Not OP, but thanks for this :-", "Those conversion rates are so ", 
"Are they>7<ý\rB\017", "in that site u get for 444 moo", "can anyone confirm how much wa", 
"Better comparison is market ca", "Yeah, the eth printer has been", 
"Sucks to see all the red but t", "I mean \034just a day\035 seems apt ", 
"If XRP is having a lawsuit fro", "The sec isn't the issue, it's ", 
"Its based in the US so the SEC", "Very sketchy ladder going up f", 
"The Hardy Boyz, The Dudley Boy", "They\031re hitting people with la", 
"\n\nyeah, whats next? FLAGS?", "Gemini Earn saved me. I was ge", 
"Thats the great hodl feature. ", "Get into a staking coin or 2. ", 
"There\031s nothing wrong with usi", "How many of you guys actually ", 
"TA in Crypto only works in hin", "I try but man it\031s hard having", 
"Tweets from billionaires blows", "Lol isn\031t that a meme word", 
"Lol don\031t think so", "More people than actually use ", "Just remembet that this whole ", 
"Coinbase also definitely wants", "Nano dropping out of top 100.", 
"Why is bat pumping!?", "BOOM BAT OVER 0.50 <‰&quot;, "ok so I'm on the verge of pull", 
"got assistance from the Pangol", "If I were to trade BTC &gt; ET", 
"Markets going crazy again, por", "Nano seems to be ready for nex", 
"Reckon nano is a good long ter", "Yes. Not going to shill but ju", 
"10$ we come", "Will BTC break 50k or get reje", "We broke it! >s", 
"LINK finally moving!", "Let's go, Cardano, show those ", "Go LINK $29 USD, go  =\200  =\200  =", 
"someone shilled me Ecomi OMI t", "Go LINK $29 USD, go  =\200  =\200  =", 
"https://vitalik.ca/general/202", "is it gonna be another bleed w", 
"Noooooooo Nan", "Any show you recommend? im get", "American Gods", 
"Do you like racing? \nDrive to ", "Rupauls Drag Race. Ive seen ev", 
"The Expanse", "The Boys", "Have you heard of The Office?", "Woohooo waiting for pay day =H", 
"I\031m relatively new to the sub.", "https://i.imgur.com/eTFToC4.jp", 
"This is okay.", "I have passed the test", "Does Kraken not have an XLM-&g", 
"Me everytime my portfolio goes", "At least now we have less to l", 
"Can anyone explain the volume ", " Per definition, volume is mea", 
"Payday, crypto day", "Yes! Come on March 15!", "Can't wait for the game Age of", 
"btw if you're on steam, whishl", "Isn\031t wish list just a fancy b", 
"yes, but steam will feature ga", "Oooh so I\031m literally wishing ", 
"When you know, you know", "what if I never know >z", "then you know that you never k", 
"^^^woah", " You buy more and figure it ou", "If nano drops out of top 100 I", 
"Really? There seems to be fair", "Well spoken! But a lot of coin", 
"it's 84 on on coinmarketcap. i", "Don\031t sweat it dude \n\nThere\031s ", 
"See y'all on the next thread !", "I'm really deliberating whethe", 
"I could definitely see another", "And what about next year?", 
"I dunno, man. I wouldn't touch", "ADA is more likely to go from ", 
"This", "![gif](giphy|3o6gDWzmAzrpi5DQU", "Except it's not that she's not", 
"A guy already does most of tha", "care to share that website?", 
"![gif](giphy|3o6MbdXxE87LYeFS7", "Having a hard time getting mys", 
"My current ones, off the top o", "Hey bro, have you been staking", 
"So I use both Cosmostation and", "where are you staking luna?", 
"terra station.\n\n\nIf you stake ", "Thanks man - invested in terra", 
"Nice man, i first got in at $0", "Hmm that's a good idea - thank", 
"If I may, where do you stake L", "https://wallet.lto.network/sig", 
"Yeah i reckon so, although wit", "&gt;I know my SNX is around 30", 
"I think so, check out the site", "I checked earlier today but th", 
"yeah fair, makes sense :)\n\n\nWe", "Roughly  how much did the gas ", 
"I didn't even notice nano had ", "1. It's 88th on Coingecko.\n2. ", 
"Don't get why you're hostile. ", "This was not hostile but a dir", 
"you sound hostile bro. he just", "The 50 has been holding strong", 
"I mean daily chart lol", "Appreciate it, I\031ll be looking", 
"I went Aave, XLM, XTZ, and add", "RIGHT! To the moon and beyond ", 
"Moon is so bearish. To Uranus!", "There's a lot of crypto here t", 
"Asian Lesbos you say >\024", "No probs bur someone told me y", 
"Mhm I hear you. Thanks for the", "Username checks out", "=\024", 
"Does it not balance out with t", "Not really, because on Honeysw", 
"But then you lose them transfe", "idk man i haven't look at it y", 
"TA is a distant secondary when", "But Coinbase is responsible fo", 
"By volume sure.  Not by end co", "The silver lining is the accel", 
"That would be a spicy market", "Can I swap it back to bitcoin ", 
"Yeah it depends on the facilit", "I am not familiar with SDEX, i", 
"Yes, it supports several asset", "Lucky to be working, had a few", 
"Hmm makes sense but I also pos", "Ahh.. Then all you can do is c", 
"I see. Thanks!", "Yeah but I mean can I see that", "Search bar, moon dist", 
"Yeah I agree with you, -90% is", "I think you didn't get the Zoo", 
"Nope haha I didn't get it, I t", "Yeah I thought it was a legit ", 
"Cool. Thanks for your insight", "Hey thanks man I am new here w", 
"Pancakeswap isn't offering me ", "I know. It's beefy I'd be worr", 
"I've tested a small amount of ", "Holding a big bag of GRT since", 
"Let's all support our beloved ", "Anyone can tell me why I\031m not", 
"300 BTC is basically all that'", "That's still 15 million usd on", 
"Considering how the 50k sell w", "Get into VET now or wait for a", 
"Get in now but leave some mone", "Get in right away. You can alw", 
"Enjin just quietly goes about ", "they have announced to have th", 
"The Agave airdrop is now worth", "Lol damn congrats if you got i", 
"I did and its a nice feeling a", "I'll be you next time >#\nWhere", 
"Honestly nowhere as I don't th", "Is atom over with?", "What\031s that even mean is it ov", 
"Your screen is upside down.", "Nope, it's exploding like an A", 
"[deleted]", "Just asking", "I mean it truly is late, but t", 
"VET (Vechain) to the moon=\200=\200=", "Well deserved too! =\200", 
"Sand continuing to kick ass. A", "Love CMC for airdropping me 25", 
"[me_irl](https://imgur.com/2qI", "Thought on going in on MATIC n", 
"He will probably keep dipping ", "I went in now because it\031s bee", 
"It's up 446% from a month ago ", "AY EL GE OH", "YU GI OH?", 
"YU YU HAKUSHO?", "Amazing how hitting the previo", "Here's go Matic again on the m", 
"?", "The chart for SLT is fuckkng i", "Yeh I\031ve been watching this on", 
"Platform is going live this mo", "I threw some money at it but I", 
"They are doing real estate as ", "I will never forget that day w", 
"Go VET! Let\031s see if this tren", "Does the Buy/Sell ratio on Coi", 
"Yes, but it's not the only mar", "Is people on this sub 100% bul", 
"You should be looking at the B", "It's still the silver standard", 
"Hella bullish", "I'm bullish on ETH for the lon", "Still massively bullish but ob", 
"The bullishness here is defini", "1. How do some people achieve ", 
"Not sure on the first point bu", "God damn VET =3", "And ffs VTHO", 
"Atom appears to be the flavor ", "See my comment here: https://w", 
"The same or better than polkad", "They have a pretty website wit", 
"Yeah looking into it more. I\031m", "Anyone have any opinions on Ko", 
"He is good. Ilikey", "How many proud ATOM holders do", "Reporting in, great to see ATO", 
"[deleted]")), row.names = c(NA, -522L), class = "data.frame")

标签: r

解决方案


正如评论中所指出的,您可以使用group_byfromdplyr来完成此操作。

首先,您可以为每个评论/句子提取关键字。然后unnest,每个关键字都在一个单独的行中,并带有日期。

然后,使用group_by包括日期和评论(以获得日期和关键字组合的频率)。summarisewith的使用n()将给出提及次数。

这是一个完整的例子:

library(tidyverse)
library(stringr)

key <- c("very", "good", "something", "is")

table_orig <- data.frame(
  date = c("01-03-21", "01-03-21", "01-04-21", "01-04-21", "01-05-21"),
  comment = c("This is very good", "This is bad", "This is good", "nothing", "something going on")
)

table_orig %>%
  mutate(word_match = map2(comment, paste(key, collapse = "\\b|\\b"), str_extract_all)) %>%
  unnest(cols = word_match) %>%
  unnest(cols = word_match) %>%
  group_by(date, word_match) %>%
  summarise(mentions = n())

输出

  date     word_match mentions
  <chr>    <chr>         <int>
1 01-03-21 good              1
2 01-03-21 is                2
3 01-03-21 very              1
4 01-04-21 good              1
5 01-04-21 is                1
6 01-05-21 something         1

推荐阅读