In this course, I would like to import the tweets from twitter using twitteR package. The purpose is to see how people see how people are reacting regarding California’s recent school shooting.
library(tm)
library(dplyr)
library(twitteR)
library(wordcloud)
library(tidyverse)
library(knitr)
library(tidytext)
api_key <- “xxx”
api_secret <- “xxx”
access_token <- “xxx”
access_secret <- “xxx”
setup_twitter_oauth(api_key, api_secret, access_token, access_secret)
## [1] "Using direct authentication"
All the given api keys and access keys were entered to extract the tweets from twitter. For the sake of security, keys are hidden.
trends <- availableTrendLocations()
print(trends)
## name country woeid
## 1 Worldwide 1
## 2 Winnipeg Canada 2972
## 3 Ottawa Canada 3369
## 4 Quebec Canada 3444
## 5 Montreal Canada 3534
## 6 Toronto Canada 4118
## 7 Edmonton Canada 8676
## 8 Calgary Canada 8775
## 9 Vancouver Canada 9807
## 10 Birmingham United Kingdom 12723
## 11 Blackpool United Kingdom 12903
## 12 Bournemouth United Kingdom 13383
## 13 Brighton United Kingdom 13911
## 14 Bristol United Kingdom 13963
## 15 Cardiff United Kingdom 15127
## 16 Coventry United Kingdom 17044
## 17 Derby United Kingdom 18114
## 18 Edinburgh United Kingdom 19344
## 19 Glasgow United Kingdom 21125
## 20 Hull United Kingdom 25211
## 21 Leeds United Kingdom 26042
## 22 Leicester United Kingdom 26062
## 23 Liverpool United Kingdom 26734
## 24 Manchester United Kingdom 28218
## 25 Middlesbrough United Kingdom 28869
## 26 Newcastle United Kingdom 30079
## 27 Nottingham United Kingdom 30720
## 28 Plymouth United Kingdom 32185
## 29 Portsmouth United Kingdom 32452
## 30 Preston United Kingdom 32566
## 31 Sheffield United Kingdom 34503
## 32 Stoke-on-Trent United Kingdom 36240
## 33 Swansea United Kingdom 36758
## 34 London United Kingdom 44418
## 35 Belfast United Kingdom 44544
## 36 Santo Domingo Dominican Republic 76456
## 37 Guatemala City Guatemala 83123
## 38 Acapulco Mexico 110978
## 39 Aguascalientes Mexico 111579
## 40 Chihuahua Mexico 115958
## 41 Mexico City Mexico 116545
## 42 Ciudad Juarez Mexico 116556
## 43 Nezahualcóyotl Mexico 116564
## 44 Culiacán Mexico 117994
## 45 Ecatepec de Morelos Mexico 118466
## 46 Guadalajara Mexico 124162
## 47 Hermosillo Mexico 124785
## 48 León Mexico 131068
## 49 Mérida Mexico 133327
## 50 Mexicali Mexico 133475
## 51 Monterrey Mexico 134047
## 52 Morelia Mexico 134091
## 53 Naucalpan de Juárez Mexico 134395
## 54 Puebla Mexico 137612
## 55 Querétaro Mexico 138045
## 56 Saltillo Mexico 141272
## 57 San Luis Potosí Mexico 144265
## 58 Tijuana Mexico 149361
## 59 Toluca Mexico 149769
## 60 Zapopan Mexico 151582
## 61 Mendoza Argentina 332471
## 62 Santiago Chile 349859
## 63 Concepcion Chile 349860
## 64 Valparaiso Chile 349861
## 65 Bogotá Colombia 368148
## 66 Cali Colombia 368149
## 67 Medellín Colombia 368150
## 68 Barranquilla Colombia 368151
## 69 Quito Ecuador 375732
## 70 Guayaquil Ecuador 375733
## 71 Caracas Venezuela 395269
## 72 Maracaibo Venezuela 395270
## 73 Maracay Venezuela 395271
## 74 Valencia Venezuela 395272
## 75 Barcelona Venezuela 395273
## 76 Ciudad Guayana Venezuela 395275
## 77 Turmero Venezuela 395277
## 78 Lima Peru 418440
## 79 Brasília Brazil 455819
## 80 Belém Brazil 455820
## 81 Belo Horizonte Brazil 455821
## 82 Curitiba Brazil 455822
## 83 Porto Alegre Brazil 455823
## 84 Recife Brazil 455824
## 85 Rio de Janeiro Brazil 455825
## 86 Salvador Brazil 455826
## 87 São Paulo Brazil 455827
## 88 Campinas Brazil 455828
## 89 Fortaleza Brazil 455830
## 90 Goiânia Brazil 455831
## 91 Manaus Brazil 455833
## 92 São Luís Brazil 455834
## 93 Guarulhos Brazil 455867
## 94 Córdoba Argentina 466861
## 95 Rosario Argentina 466862
## 96 Barquisimeto Venezuela 468382
## 97 Maturín Venezuela 468384
## 98 Buenos Aires Argentina 468739
## 99 Gdansk Poland 493417
## 100 Kraków Poland 502075
## 101 Lodz Poland 505120
## 102 Poznan Poland 514048
## 103 Warsaw Poland 523920
## 104 Wroclaw Poland 526363
## 105 Vienna Austria 551801
## 106 Cork Ireland 560472
## 107 Dublin Ireland 560743
## 108 Galway Ireland 560912
## 109 Bordeaux France 580778
## 110 Lille France 608105
## 111 Lyon France 609125
## 112 Marseille France 610264
## 113 Montpellier France 612977
## 114 Nantes France 613858
## 115 Paris France 615702
## 116 Rennes France 619163
## 117 Strasbourg France 627791
## 118 Toulouse France 628886
## 119 Berlin Germany 638242
## 120 Bremen Germany 641142
## 121 Dortmund Germany 645458
## 122 Dresden Germany 645686
## 123 Dusseldorf Germany 646099
## 124 Essen Germany 648820
## 125 Frankfurt Germany 650272
## 126 Hamburg Germany 656958
## 127 Cologne Germany 667931
## 128 Leipzig Germany 671072
## 129 Munich Germany 676757
## 130 Stuttgart Germany 698064
## 131 Bologna Italy 711080
## 132 Genoa Italy 716085
## 133 Milan Italy 718345
## 134 Naples Italy 719258
## 135 Palermo Italy 719846
## 136 Rome Italy 721943
## 137 Turin Italy 725003
## 138 Den Haag Netherlands 726874
## 139 Amsterdam Netherlands 727232
## 140 Rotterdam Netherlands 733075
## 141 Utrecht Netherlands 734047
## 142 Barcelona Spain 753692
## 143 Bilbao Spain 754542
## 144 Las Palmas Spain 764814
## 145 Madrid Spain 766273
## 146 Malaga Spain 766356
## 147 Murcia Spain 768026
## 148 Palma Spain 769293
## 149 Seville Spain 774508
## 150 Valencia Spain 776688
## 151 Zaragoza Spain 779063
## 152 Geneva Switzerland 782538
## 153 Lausanne Switzerland 783058
## 154 Zurich Switzerland 784794
## 155 Brest Belarus 824382
## 156 Grodno Belarus 825848
## 157 Gomel Belarus 825978
## 158 Minsk Belarus 834463
## 159 Riga Latvia 854823
## 160 Bergen Norway 857105
## 161 Oslo Norway 862592
## 162 Gothenburg Sweden 890869
## 163 Stockholm Sweden 906057
## 164 Dnipropetrovsk Ukraine 918981
## 165 Donetsk Ukraine 919163
## 166 Kharkiv Ukraine 922137
## 167 Kyiv Ukraine 924938
## 168 Lviv Ukraine 924943
## 169 Odesa Ukraine 929398
## 170 Zaporozhye Ukraine 939628
## 171 Athens Greece 946738
## 172 Thessaloniki Greece 963291
## 173 Bekasi Indonesia 1030077
## 174 Depok Indonesia 1032539
## 175 Pekanbaru Indonesia 1040779
## 176 Surabaya Indonesia 1044316
## 177 Makassar Indonesia 1046138
## 178 Bandung Indonesia 1047180
## 179 Jakarta Indonesia 1047378
## 180 Medan Indonesia 1047908
## 181 Palembang Indonesia 1048059
## 182 Semarang Indonesia 1048324
## 183 Tangerang Indonesia 1048536
## 184 Singapore Singapore 1062617
## 185 Perth Australia 1098081
## 186 Adelaide Australia 1099805
## 187 Brisbane Australia 1100661
## 188 Canberra Australia 1100968
## 189 Darwin Australia 1101597
## 190 Melbourne Australia 1103816
## 191 Sydney Australia 1105779
## 192 Kitakyushu Japan 1110809
## 193 Saitama Japan 1116753
## 194 Chiba Japan 1117034
## 195 Fukuoka Japan 1117099
## 196 Hamamatsu Japan 1117155
## 197 Hiroshima Japan 1117227
## 198 Kawasaki Japan 1117502
## 199 Kobe Japan 1117545
## 200 Kumamoto Japan 1117605
## 201 Nagoya Japan 1117817
## 202 Niigata Japan 1117881
## 203 Sagamihara Japan 1118072
## 204 Sapporo Japan 1118108
## 205 Sendai Japan 1118129
## 206 Takamatsu Japan 1118285
## 207 Tokyo Japan 1118370
## 208 Yokohama Japan 1118550
## 209 Goyang Korea 1130853
## 210 Yongin Korea 1132094
## 211 Ansan Korea 1132444
## 212 Bucheon Korea 1132445
## 213 Busan Korea 1132447
## 214 Changwon Korea 1132449
## 215 Daegu Korea 1132466
## 216 Gwangju Korea 1132481
## 217 Incheon Korea 1132496
## 218 Seongnam Korea 1132559
## 219 Suwon Korea 1132567
## 220 Ulsan Korea 1132578
## 221 Seoul Korea 1132599
## 222 Kajang Malaysia 1141268
## 223 Ipoh Malaysia 1154679
## 224 Johor Bahru Malaysia 1154698
## 225 Klang Malaysia 1154726
## 226 Kuala Lumpur Malaysia 1154781
## 227 Calocan Philippines 1167715
## 228 Makati Philippines 1180689
## 229 Pasig Philippines 1187115
## 230 Taguig Philippines 1195098
## 231 Antipolo Philippines 1198785
## 232 Cagayan de Oro Philippines 1199002
## 233 Cebu City Philippines 1199079
## 234 Davao City Philippines 1199136
## 235 Manila Philippines 1199477
## 236 Quezon City Philippines 1199682
## 237 Zamboanga City Philippines 1199980
## 238 Bangkok Thailand 1225448
## 239 Hanoi Vietnam 1236594
## 240 Hai Phong Vietnam 1236690
## 241 Can Tho Vietnam 1252351
## 242 Da Nang Vietnam 1252376
## 243 Ho Chi Minh City Vietnam 1252431
## 244 Algiers Algeria 1253079
## 245 Accra Ghana 1326075
## 246 Kumasi Ghana 1330595
## 247 Benin City Nigeria 1387660
## 248 Ibadan Nigeria 1393672
## 249 Kaduna Nigeria 1396439
## 250 Kano Nigeria 1396803
## 251 Lagos Nigeria 1398823
## 252 Port Harcourt Nigeria 1404447
## 253 Giza Egypt 1521643
## 254 Cairo Egypt 1521894
## 255 Alexandria Egypt 1522006
## 256 Mombasa Kenya 1528335
## 257 Nairobi Kenya 1528488
## 258 Durban South Africa 1580913
## 259 Johannesburg South Africa 1582504
## 260 Port Elizabeth South Africa 1586614
## 261 Pretoria South Africa 1586638
## 262 Soweto South Africa 1587677
## 263 Cape Town South Africa 1591691
## 264 Medina Saudi Arabia 1937801
## 265 Dammam Saudi Arabia 1939574
## 266 Riyadh Saudi Arabia 1939753
## 267 Jeddah Saudi Arabia 1939873
## 268 Mecca Saudi Arabia 1939897
## 269 Sharjah United Arab Emirates 1940119
## 270 Abu Dhabi United Arab Emirates 1940330
## 271 Dubai United Arab Emirates 1940345
## 272 Haifa Israel 1967449
## 273 Tel Aviv Israel 1968212
## 274 Jerusalem Israel 1968222
## 275 Amman Jordan 1968902
## 276 Chelyabinsk Russia 1997422
## 277 Khabarovsk Russia 2018708
## 278 Krasnodar Russia 2028717
## 279 Krasnoyarsk Russia 2029043
## 280 Samara Russia 2077746
## 281 Voronezh Russia 2108210
## 282 Yekaterinburg Russia 2112237
## 283 Irkutsk Russia 2121040
## 284 Kazan Russia 2121267
## 285 Moscow Russia 2122265
## 286 Nizhny Novgorod Russia 2122471
## 287 Novosibirsk Russia 2122541
## 288 Omsk Russia 2122641
## 289 Perm Russia 2122814
## 290 Rostov-on-Don Russia 2123177
## 291 Saint Petersburg Russia 2123260
## 292 Ufa Russia 2124045
## 293 Vladivostok Russia 2124288
## 294 Volgograd Russia 2124298
## 295 Karachi Pakistan 2211096
## 296 Lahore Pakistan 2211177
## 297 Multan Pakistan 2211269
## 298 Rawalpindi Pakistan 2211387
## 299 Faisalabad Pakistan 2211574
## 300 Muscat Oman 2268284
## 301 Nagpur India 2282863
## 302 Lucknow India 2295377
## 303 Kanpur India 2295378
## 304 Patna India 2295381
## 305 Ranchi India 2295383
## 306 Kolkata India 2295386
## 307 Srinagar India 2295387
## 308 Amritsar India 2295388
## 309 Jaipur India 2295401
## 310 Ahmedabad India 2295402
## 311 Rajkot India 2295404
## 312 Surat India 2295405
## 313 Bhopal India 2295407
## 314 Indore India 2295408
## 315 Thane India 2295410
## 316 Mumbai India 2295411
## 317 Pune India 2295412
## 318 Hyderabad India 2295414
## 319 Bangalore India 2295420
## 320 Chennai India 2295424
## 321 Mersin Turkey 2323778
## 322 Adana Turkey 2343678
## 323 Ankara Turkey 2343732
## 324 Antalya Turkey 2343733
## 325 Bursa Turkey 2343843
## 326 Diyarbakir Turkey 2343932
## 327 Eskisehir Turkey 2343980
## 328 Gaziantep Turkey 2343999
## 329 Istanbul Turkey 2344116
## 330 Izmir Turkey 2344117
## 331 Kayseri Turkey 2344174
## 332 Konya Turkey 2344210
## 333 Okinawa Japan 2345896
## 334 Daejeon Korea 2345975
## 335 Auckland New Zealand 2348079
## 336 Albuquerque United States 2352824
## 337 Atlanta United States 2357024
## 338 Austin United States 2357536
## 339 Baltimore United States 2358820
## 340 Baton Rouge United States 2359991
## 341 Birmingham United States 2364559
## 342 Boston United States 2367105
## 343 Charlotte United States 2378426
## 344 Chicago United States 2379574
## 345 Cincinnati United States 2380358
## 346 Cleveland United States 2381475
## 347 Colorado Springs United States 2383489
## 348 Columbus United States 2383660
## 349 Dallas-Ft. Worth United States 2388929
## 350 Denver United States 2391279
## 351 Detroit United States 2391585
## 352 El Paso United States 2397816
## 353 Fresno United States 2407517
## 354 Greensboro United States 2414469
## 355 Harrisburg United States 2418046
## 356 Honolulu United States 2423945
## 357 Houston United States 2424766
## 358 Indianapolis United States 2427032
## 359 Jackson United States 2428184
## 360 Jacksonville United States 2428344
## 361 Kansas City United States 2430683
## 362 Las Vegas United States 2436704
## 363 Long Beach United States 2441472
## 364 Los Angeles United States 2442047
## 365 Louisville United States 2442327
## 366 Memphis United States 2449323
## 367 Mesa United States 2449808
## 368 Miami United States 2450022
## 369 Milwaukee United States 2451822
## 370 Minneapolis United States 2452078
## 371 Nashville United States 2457170
## 372 New Haven United States 2458410
## 373 New Orleans United States 2458833
## 374 New York United States 2459115
## 375 Norfolk United States 2460389
## 376 Oklahoma City United States 2464592
## 377 Omaha United States 2465512
## 378 Orlando United States 2466256
## 379 Philadelphia United States 2471217
## 380 Phoenix United States 2471390
## 381 Pittsburgh United States 2473224
## 382 Portland United States 2475687
## 383 Providence United States 2477058
## 384 Raleigh United States 2478307
## 385 Richmond United States 2480894
## 386 Sacramento United States 2486340
## 387 St. Louis United States 2486982
## 388 Salt Lake City United States 2487610
## 389 San Antonio United States 2487796
## 390 San Diego United States 2487889
## 391 San Francisco United States 2487956
## 392 San Jose United States 2488042
## 393 Seattle United States 2490383
## 394 Tallahassee United States 2503713
## 395 Tampa United States 2503863
## 396 Tucson United States 2508428
## 397 Virginia Beach United States 2512636
## 398 Washington United States 2514815
## 399 Osaka Japan 15015370
## 400 Kyoto Japan 15015372
## 401 Delhi India 20070458
## 402 United Arab Emirates United Arab Emirates 23424738
## 403 Algeria Algeria 23424740
## 404 Argentina Argentina 23424747
## 405 Australia Australia 23424748
## 406 Austria Austria 23424750
## 407 Bahrain Bahrain 23424753
## 408 Belgium Belgium 23424757
## 409 Belarus Belarus 23424765
## 410 Brazil Brazil 23424768
## 411 Canada Canada 23424775
## 412 Chile Chile 23424782
## 413 Colombia Colombia 23424787
## 414 Denmark Denmark 23424796
## 415 Dominican Republic Dominican Republic 23424800
## 416 Ecuador Ecuador 23424801
## 417 Egypt Egypt 23424802
## 418 Ireland Ireland 23424803
## 419 France France 23424819
## 420 Ghana Ghana 23424824
## 421 Germany Germany 23424829
## 422 Greece Greece 23424833
## 423 Guatemala Guatemala 23424834
## 424 Indonesia Indonesia 23424846
## 425 India India 23424848
## 426 Israel Israel 23424852
## 427 Italy Italy 23424853
## 428 Japan Japan 23424856
## 429 Jordan Jordan 23424860
## 430 Kenya Kenya 23424863
## 431 Korea Korea 23424868
## 432 Kuwait Kuwait 23424870
## 433 Lebanon Lebanon 23424873
## 434 Latvia Latvia 23424874
## 435 Oman Oman 23424898
## 436 Mexico Mexico 23424900
## 437 Malaysia Malaysia 23424901
## 438 Nigeria Nigeria 23424908
## 439 Netherlands Netherlands 23424909
## 440 Norway Norway 23424910
## 441 New Zealand New Zealand 23424916
## 442 Peru Peru 23424919
## 443 Pakistan Pakistan 23424922
## 444 Poland Poland 23424923
## 445 Panama Panama 23424924
## 446 Portugal Portugal 23424925
## 447 Qatar Qatar 23424930
## 448 Philippines Philippines 23424934
## 449 Puerto Rico Puerto Rico 23424935
## 450 Russia Russia 23424936
## 451 Saudi Arabia Saudi Arabia 23424938
## 452 South Africa South Africa 23424942
## 453 Singapore Singapore 23424948
## 454 Spain Spain 23424950
## 455 Sweden Sweden 23424954
## 456 Switzerland Switzerland 23424957
## 457 Thailand Thailand 23424960
## 458 Turkey Turkey 23424969
## 459 United Kingdom United Kingdom 23424975
## 460 Ukraine Ukraine 23424976
## 461 United States United States 23424977
## 462 Venezuela Venezuela 23424982
## 463 Vietnam Vietnam 23424984
## 464 Petaling Malaysia 56013632
## 465 Hulu Langat Malaysia 56013645
## 466 Ahsa Saudi Arabia 56120136
## 467 Okayama Japan 90036018
trends_sandiego <- getTrends(2487889)
trends_sandiego
## name
## 1 Nunes
## 2 Melo
## 3 Volker
## 4 Rivers
## 5 Vindman
## 6 #kcvslac
## 7 Gym Jordan
## 8 Morrison
## 9 #HistoryWillRememberTrump
## 10 #TheFlash
## 11 Cornette
## 12 #Empire
## 13 #VoiceResults
## 14 Jaxson Hayes
## 15 Anthony Lamb
## 16 Geraldo
## 17 Poch
## 18 Mourinho
## 19 Eichel
## 20 Franklin Graham
## 21 Morgan Frost
## 22 Uncle Nicky
## 23 Tottenham
## 24 Rodney Hood
## 25 Noah Hawley
## 26 #NWAPowerrr
## 27 #YIAY10s
## 28 #AEWDark
## 29 #SuperMinNYC
## 30 #TheResident
## 31 #IMPACTonAXSTV
## 32 #shsuringceremony
## 33 #TeenMom2
## 34 #SWTAtlanta
## 35 #mixedish
## 36 #FBICBS
## 37 #WhyImThankfulForPizza
## 38 #IPrepareForBlackFridayBy
## 39 #OakIslandCursers
## 40 #YoungAndPregnant
## 41 #PORvsNOP
## 42 #TheConners
## 43 #ClimateChangeTunes
## 44 #ncis
## 45 #lightningstrikes
## 46 #CFPlayoff
## 47 #CUBvUSA
## 48 #2009v2019
## 49 #ResilienceWeek
## 50 #AppleEDUchat
## url
## 1 http://twitter.com/search?q=Nunes
## 2 http://twitter.com/search?q=Melo
## 3 http://twitter.com/search?q=Volker
## 4 http://twitter.com/search?q=Rivers
## 5 http://twitter.com/search?q=Vindman
## 6 http://twitter.com/search?q=%23kcvslac
## 7 http://twitter.com/search?q=%22Gym+Jordan%22
## 8 http://twitter.com/search?q=Morrison
## 9 http://twitter.com/search?q=%23HistoryWillRememberTrump
## 10 http://twitter.com/search?q=%23TheFlash
## 11 http://twitter.com/search?q=Cornette
## 12 http://twitter.com/search?q=%23Empire
## 13 http://twitter.com/search?q=%23VoiceResults
## 14 http://twitter.com/search?q=%22Jaxson+Hayes%22
## 15 http://twitter.com/search?q=%22Anthony+Lamb%22
## 16 http://twitter.com/search?q=Geraldo
## 17 http://twitter.com/search?q=Poch
## 18 http://twitter.com/search?q=Mourinho
## 19 http://twitter.com/search?q=Eichel
## 20 http://twitter.com/search?q=%22Franklin+Graham%22
## 21 http://twitter.com/search?q=%22Morgan+Frost%22
## 22 http://twitter.com/search?q=%22Uncle+Nicky%22
## 23 http://twitter.com/search?q=Tottenham
## 24 http://twitter.com/search?q=%22Rodney+Hood%22
## 25 http://twitter.com/search?q=%22Noah+Hawley%22
## 26 http://twitter.com/search?q=%23NWAPowerrr
## 27 http://twitter.com/search?q=%23YIAY10s
## 28 http://twitter.com/search?q=%23AEWDark
## 29 http://twitter.com/search?q=%23SuperMinNYC
## 30 http://twitter.com/search?q=%23TheResident
## 31 http://twitter.com/search?q=%23IMPACTonAXSTV
## 32 http://twitter.com/search?q=%23shsuringceremony
## 33 http://twitter.com/search?q=%23TeenMom2
## 34 http://twitter.com/search?q=%23SWTAtlanta
## 35 http://twitter.com/search?q=%23mixedish
## 36 http://twitter.com/search?q=%23FBICBS
## 37 http://twitter.com/search?q=%23WhyImThankfulForPizza
## 38 http://twitter.com/search?q=%23IPrepareForBlackFridayBy
## 39 http://twitter.com/search?q=%23OakIslandCursers
## 40 http://twitter.com/search?q=%23YoungAndPregnant
## 41 http://twitter.com/search?q=%23PORvsNOP
## 42 http://twitter.com/search?q=%23TheConners
## 43 http://twitter.com/search?q=%23ClimateChangeTunes
## 44 http://twitter.com/search?q=%23ncis
## 45 http://twitter.com/search?q=%23lightningstrikes
## 46 http://twitter.com/search?q=%23CFPlayoff
## 47 http://twitter.com/search?q=%23CUBvUSA
## 48 http://twitter.com/search?q=%232009v2019
## 49 http://twitter.com/search?q=%23ResilienceWeek
## 50 http://twitter.com/search?q=%23AppleEDUchat
## query woeid
## 1 Nunes 2487889
## 2 Melo 2487889
## 3 Volker 2487889
## 4 Rivers 2487889
## 5 Vindman 2487889
## 6 %23kcvslac 2487889
## 7 %22Gym+Jordan%22 2487889
## 8 Morrison 2487889
## 9 %23HistoryWillRememberTrump 2487889
## 10 %23TheFlash 2487889
## 11 Cornette 2487889
## 12 %23Empire 2487889
## 13 %23VoiceResults 2487889
## 14 %22Jaxson+Hayes%22 2487889
## 15 %22Anthony+Lamb%22 2487889
## 16 Geraldo 2487889
## 17 Poch 2487889
## 18 Mourinho 2487889
## 19 Eichel 2487889
## 20 %22Franklin+Graham%22 2487889
## 21 %22Morgan+Frost%22 2487889
## 22 %22Uncle+Nicky%22 2487889
## 23 Tottenham 2487889
## 24 %22Rodney+Hood%22 2487889
## 25 %22Noah+Hawley%22 2487889
## 26 %23NWAPowerrr 2487889
## 27 %23YIAY10s 2487889
## 28 %23AEWDark 2487889
## 29 %23SuperMinNYC 2487889
## 30 %23TheResident 2487889
## 31 %23IMPACTonAXSTV 2487889
## 32 %23shsuringceremony 2487889
## 33 %23TeenMom2 2487889
## 34 %23SWTAtlanta 2487889
## 35 %23mixedish 2487889
## 36 %23FBICBS 2487889
## 37 %23WhyImThankfulForPizza 2487889
## 38 %23IPrepareForBlackFridayBy 2487889
## 39 %23OakIslandCursers 2487889
## 40 %23YoungAndPregnant 2487889
## 41 %23PORvsNOP 2487889
## 42 %23TheConners 2487889
## 43 %23ClimateChangeTunes 2487889
## 44 %23ncis 2487889
## 45 %23lightningstrikes 2487889
## 46 %23CFPlayoff 2487889
## 47 %23CUBvUSA 2487889
## 48 %232009v2019 2487889
## 49 %23ResilienceWeek 2487889
## 50 %23AppleEDUchat 2487889
usertweet <- getUser('wesmckinn') # Accessing the user
usertweet
## [1] "wesmckinn"
wesmckinney <- userTimeline(usertweet, n=10) # Getting 10 tweets from user Wes McKinney
wesmckinney_df <- twListToDF(wesmckinney) # Changing the list into dataframe using twitteR's function
wesmckinney_df
## text
## 1 @minrk You should talk with @KrisztianSzucs about our Crossbow system for @ApacheArrow wheels, it could be a help
## 2 At this point the Trumpian defense to what happened is like saying it's not murder unless you say "I am murdering y… https://t.co/YTbeMuHMsw
## 3 Note: since I wrote that GitHub now acks code reviews as contributions, which is good
## 4 The next time you consider measuring someone's productivity based on their GitHub contribution calendar, I refer yo… https://t.co/tx5pjwzeup
## 5 Reminder to get upgraded! https://t.co/4lo7TcSPm2
## 6 @jonathankennell Clutter is excellent
## 7 @BrianInLaw hm, don't recall!
## favorited favoriteCount replyToSN created truncated
## 1 FALSE 2 minrk 2019-11-14 11:05:34 FALSE
## 2 FALSE 47 <NA> 2019-11-13 17:38:05 TRUE
## 3 FALSE 3 wesmckinn 2019-11-07 16:22:50 FALSE
## 4 FALSE 36 <NA> 2019-11-07 16:22:19 TRUE
## 5 FALSE 16 <NA> 2019-11-06 17:29:44 FALSE
## 6 FALSE 0 jonathankennell 2019-11-06 03:22:29 FALSE
## 7 FALSE 0 BrianInLaw 2019-10-31 17:29:25 FALSE
## replyToSID id replyToUID
## 1 1194911841509621760 1194934391010734080 15423006
## 2 <NA> 1194670783336833025 <NA>
## 3 1192477388036288513 1192477517875220480 115494880
## 4 <NA> 1192477388036288513 <NA>
## 5 <NA> 1192131967208378372 <NA>
## 6 1191912471726034944 1191918749953273857 15580417
## 7 1189866000633810944 1189957562151645184 213019436
## statusSource
## 1 <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## 2 <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## 3 <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## 4 <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## 5 <a href="https://about.twitter.com/products/tweetdeck" rel="nofollow">TweetDeck</a>
## 6 <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## 7 <a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>
## screenName retweetCount isRetweet retweeted longitude latitude
## 1 wesmckinn 0 FALSE FALSE NA NA
## 2 wesmckinn 1 FALSE FALSE NA NA
## 3 wesmckinn 0 FALSE FALSE NA NA
## 4 wesmckinn 7 FALSE FALSE NA NA
## 5 wesmckinn 9 FALSE FALSE NA NA
## 6 wesmckinn 0 FALSE FALSE NA NA
## 7 wesmckinn 0 FALSE FALSE NA NA
Using library twitteR package, I extracted the tweets for which I used searchTwitter function which is used to search for the keywords. Furthermore, I limited number of tweets to 1000 with the tweets posted in English language.
# Now let's start extracting tweets regarding impeachment inquiry by using few trending tweets
tweets <- searchTwitter('#schoolshooting, #guncontrol', n=1000, lang = 'en')
# Converting tweets into dataframe
school_shooting <- twListToDF(tweets)
school_shooting_text <- school_shooting$text # This vector contain only tweets
With the free access, I believe I can only access tweets up to 30 days that’s why I had to download the text into local directory. There are two files. First is the main file while second dataset contains only tweets.
# Writing both files into csv
write.csv(school_shooting, file='shooting_file.csv', row.names=FALSE)
write.csv(school_shooting_text, file='shooting_tweetsonly.csv', row.names=FALSE)
# Reading the csv file from local directory
shooting_tweets <- read.csv('shooting_tweetsonly.csv', row.names=NULL, stringsAsFactors = FALSE)
Corpus has been used to clean the data using tm package to remove numbers, punctuations, extra spaces and some commonly used English words. Furthermore, “word” dataset was later converted into matrix to use wordcloud.
words <- Corpus(VectorSource(shooting_tweets$x)) # Saving the tweets in vector 'words' while x is column's name which was given randomly while importing
words <- tm_map(words, tolower)
## Warning in tm_map.SimpleCorpus(words, tolower): transformation drops
## documents
words <- tm_map(words, removeNumbers)
## Warning in tm_map.SimpleCorpus(words, removeNumbers): transformation drops
## documents
words <- tm_map(words, removePunctuation)
## Warning in tm_map.SimpleCorpus(words, removePunctuation): transformation
## drops documents
words <- tm_map(words, stripWhitespace)
## Warning in tm_map.SimpleCorpus(words, stripWhitespace): transformation
## drops documents
words <- tm_map(words, removeWords, stopwords("english"))
## Warning in tm_map.SimpleCorpus(words, removeWords, stopwords("english")):
## transformation drops documents
words <- tm_map(words, removeWords, c("will")) # This sentence would be helpful for later to remove any unnecessary words
## Warning in tm_map.SimpleCorpus(words, removeWords, c("will")):
## transformation drops documents
# Now let's build a matrix and dataframe to show the number of words to make wordcloud
tdm <- TermDocumentMatrix((words))
m <- as.matrix(tdm)
v <- sort(rowSums(m), decreasing=TRUE)
d <- data.frame(word= names(v), freq=v)
head(d,20)
## word freq
## guncontrol guncontrol 173
## schoolshooting schoolshooting 150
## another another 62
## amp amp 57
## guncontrolnow guncontrolnow 52
## gun gun 51
## school school 45
## santa santa 43
## clarita clarita 41
## nra nra 37
## shooting shooting 36
## today today 29
## california california 27
## enough enough 27
## now now 25
## yet yet 25
## saugus saugus 24
## kids kids 24
## santaclarita santaclarita 22
## government government 22
The above table shows that guncontrol, schoolshooting, another, amp and guncontrolnow are the most frequently used words by users.
For the sake of visualization, we are going to use wordcloud and sentiment analysis graphically to see the data in a better way.
set.seed(3321)
wordcloud(words=d$word, freq=d$freq, min.freq=10, max.words =200, random.order=FALSE, decreasing= TRUE, rot.per=0.05, colors=brewer.pal(10,"Dark2"))
Above wordcloud shows most frequently used which we have seen already in the above table. It shows the words more clearly.
# Using sentiment analysis to see people's reaction
ss_tdm <- tidy(tdm)
ss_senti <- ss_tdm %>%
inner_join(get_sentiments("bing"), by=c(term="word"))
ss_senti %>%
count(sentiment, term, wt=count) %>%
ungroup() %>%
filter(n>= 5) %>%
mutate(n= ifelse(sentiment=="negative", -n, n)) %>%
mutate(term=reorder(term,n)) %>%
ggplot(aes(term, n, fill=sentiment))+ geom_bar(stat="identity")+ylab("Sentiment analysis on school shooting")+coord_flip()
Sentiment analysis was used to see people’s sentiment regarding the school shooting which recently happened in California. It shows that people are positive regarding enough and supporting which is I believe in context of controlling the gun control laws and background check while excuse and sad were used in anger and sadness by the users. People are overall angry about the incident and they want better gun control laws.