Starten wir!

In diesen Slides wollen wir uns mit Textdaten beschäftigen. Hieru lernen wir grundlegende Funktionen im Umgang mit Textdaten kennen. Eins vorweg: Textdaten sind immer schwieriger zu händeln als numerische Daten. Der Umgang in R ist zwar ganz passabel, aber optimal ist anders.

In R gibt es dazu ein gutes Paket innerhalb des tidyverse - stringr. Gemeinsam mit regular expressions können viele Transformationen durchgeführt werden.

Es bedarf aber einer erhöhten Lernbereitschaft (im Vergleich zu anderen Paketen und Funktionen).

stringr

Wie bekommen wir Textdaten?

Als Textdaten fallen einen natürlich sofort Tweets ein, oder für die Offline-Leute z.B. Wahlprogramme. Im Kurs Quantitative Text- und Inhaltsanalyse wird die Erhebung dieser Daten über die sogenannte API (Application Programming Interferenc) beigebracht. Wir überspringen jetzt diesen Schritt, da wir uns erstmal nur mit den grundlegenden Funktionen der Bearbeitung von Textvariablen in R beschäftigen wollen.

Dafür habe ich die letzten Tweets der hervorragenden Sendung The Great British Bake Off über die Twitter-API geladen (Stand: 22. September 2021). Wir werden im nachfolgenden verschiedene Schritte durchgehen, wie wir Textdaten mit den Funktionen des Pakets stringr bearbeiten können.

Package und Daten laden

Zuerst laden wir wieder tidyverse bzw. installieren es, wenn es noch nicht installiert ist:

# install.packages("tidyverse")
library("tidyverse")

# alternativ: 
# install.packages("stringr")
# library("stringr")

Anschließend laden wir den Datensatz gbbo ins environment.

gbbo <- readRDS("../datasets/gbbo.rds") # oder eigenen Pfad!

Schauen wir uns nun den Datensatz mal an:

gbbo

Der Datensatz umfasst in Gänze 90 Variablen, für uns ist in diesem Fall aber nur eine einzige Variable erstmal relevant - nämlich text. In text befindet sich der Text des jeweiligen Tweets. Und wir möchten ja nun Text bearbeiten mit stringr.

Der einfachheithalber speichern wir diese eine Variable als einzelnen Vektor, sodass wir uns im nachfolgenden die Angabe des Datensatzes sparen.

tweet <- gbbo$text

head(tweet,
     n = 100)
  [1] "There are two key stages in life. Before you discover Malt Loaf and everything afterwards. #GBBO https://t.co/4LSBTfhiR4"                                                                            
  [2] "@GeekEnby We all deserve a little ray of sunshine at least once a week. 🙏 #GBBO"                                                                                                                    
  [3] "Well, it looks like malt loaf awareness isn’t in bad shape at all! #GBBO https://t.co/4LSBTfhiR4"                                                                                                    
  [4] "We bow down before all of you wonderful people who joined us last night for the return of #GBBO https://t.co/q5ebtUTMAi"                                                                             
  [5] "When your new crush starts ghosting you… #GBBO https://t.co/XwcVnIBfCi"                                                                                                                              
  [6] "About last night… #GBBO https://t.co/GulcsAdnJM"                                                                                                                                                     
  [7] "Thanks for tweeting along with us tonight on the return of The Great British Bake Off! You really do make the #GBBO Twitter experience pure joy. ❤️❤️ #GBBO https://t.co/PnlzGRb0Mb"                   
  [8] "Let’s get ready to crumble! It’s crunch time next Tuesday as Biscuit Week hits the Bake Off Tent! #GBBO https://t.co/xGMRw30WA8"                                                                     
  [9] "@philringsell @PrueLeith Tres Bon-o. #GBBO https://t.co/vGlYoVXeCs"                                                                                                                                  
 [10] "@Shambom65 True. Absolutely true. #GBBO"                                                                                                                                                             
 [11] "Disclaimer: This is not a percentage in terms of flavour balance. It's how many people like Mint Choc Ch... *stares directly to camera* #GBBO"                                                       
 [12] "It's official! Mint Choc Chip has now been certified as being 67.6% Mint. #GBBO https://t.co/vUovHq8iqD"                                                                                             
 [13] "“Although my time in the Bake Off tent may have been short, it certainly has been very sweet.”\nHere’s Tom’s lovely letter to all you #GBBO fans. https://t.co/JgKlfD8WDg"                           
 [14] "No expense was spared to turn the Bake Off Tent into an anti-gravity zone for tonight’s show. #GBBO https://t.co/8MEF5qemLe"                                                                         
 [15] "Mark our words... 😉 #GBBO https://t.co/5s8w8XjVDR"                                                                                                                                                  
 [16] "Poor Tom! So gracious in defeat - and we’re so sad to see him go. But what time we did have with him in the Tent has been utterly fantastic. ❤️ #GBBO https://t.co/qrz9FWe9sc"                        
 [17] "He came. He self-judged. He won. Here’s how Star Baker Jürgen conquered Cake Week! #GBBO https://t.co/kKJFeUvAUk"                                                                                    
 [18] "One episode into Bake Off 2021 - and George is already turning into one of the most relatable bakers we’ve ever met. #GBBO https://t.co/pKuPc4f4IZ"                                                  
 [19] "When Jürgen’s victory call home didn’t quite go to plan… #GBBO https://t.co/RzgdhqFmnb"                                                                                                              
 [20] "Do you have cuppa hot chocolate in a medium-sized tinted glass mug? #GBBO https://t.co/xbWbk4JJnL"                                                                                                   
 [21] "A massive thumbs up for Star Baker Jürgen from the judges! 👍👍 #GBBO https://t.co/TgZUbqcSSb"                                                                                                       
 [22] "Jürgen’s ‘Night Time Reading’ Anti-Gravity Showstopper Cake. #GBBO https://t.co/42rFL35omD"                                                                                                          
 [23] "Giuseppe’s ‘Jack and The Beanstalk’ Anti-Gravity Showstopper Cake. #GBBO https://t.co/WxwxZquxPi"                                                                                                    
 [24] "Tom’s Classically Spectacular Anti-Gravity Showstopper Cake. #GBBO https://t.co/JSdm6M3Zt3"                                                                                                          
 [25] "Rochica’s Anti-Gravity Apple Cake Showstopper. #GBBO https://t.co/60Vanr8fnB"                                                                                                                        
 [26] "Crystelle’s ‘Bouq-cake’ of Flowers Anti-Gravity Showstopper Cake. #GBBO https://t.co/lIlsAPVK6n"                                                                                                     
 [27] "Freya’s ‘Flower Potty’ Anti-Gravity Showstopper Cake. #GBBO https://t.co/OFrqBxqiWf"                                                                                                                 
 [28] "Lizzie’s Nana’s Peaches &amp; Cream Anti-Gravity Showstopper Cake. #GBBO https://t.co/4t4c1VxxcS"                                                                                                    
 [29] "FACT: The gravitational pull in the Bake Off Tent is the strongest on the planet. #GBBO https://t.co/LyC3cPKkFG"                                                                                     
 [30] "@courtsthomasx Congratulations! 🥳👼 #GBBO"                                                                                                                                                          
 [31] "Amanda’s Elderflower &amp; Lemon Surf Explosion Anti-Gravity Showstopper Cake. #GBBO https://t.co/6C8pShegFt"                                                                                        
 [32] "Jairzeno’s ‘Kick The Can’ Anti-Gravity Showstopper Cake. #GBBO https://t.co/M2ntxYuy8X"                                                                                                              
 [33] "Maggie’s Ice Cream Cornet Anti-Gravity Showstopper Cake. #GBBO https://t.co/qyWu2RSCOi"                                                                                                              
 [34] "George’s Greek Table Anti-Gravity Showstopper Cake. #GBBO https://t.co/Yt0tSlogb2"                                                                                                                   
 [35] "This anti-gravity cake had one job... #GBBO https://t.co/pSMD8uILn4"                                                                                                                                 
 [36] "Any Chuckle Brothers homage on primetime television is music to our ears. ❤️❤️ #GBBO https://t.co/ZZmdn49fJQ"                                                                                          
 [37] "Chigs’ ‘Where It All Began’ Anti-Gravity Showstopper Cake. #GBBO https://t.co/1scORW3clT"                                                                                                            
 [38] "The pure joy of baking something you’re proud of. Can’t be beaten. #GBBO https://t.co/yB6TTaWcZS"                                                                                                    
 [39] "Unfortunately Prue2 is already the name of @PrueLeith's U2 tribute band. #GBBO https://t.co/Pm97wOWQNn"                                                                                              
 [40] "😥😥😥 #GBBO https://t.co/8jug1xBlL1"                                                                                                                                                                
 [41] "The first rule of Bake Off:\nDo not stare back at Paul\n\nThe second rule of Bake Off:\nDo not stare back at Paul\n#GBBO https://t.co/XbZqAE5mjA"                                                    
 [42] "@psyche1701 Maybe. #GBBO"                                                                                                                                                                            
 [43] "WISDOMOUS! #GBBO"                                                                                                                                                                                    
 [44] "@JadeForster A treat for the whole family. 🥳 #GBBO"                                                                                                                                                 
 [45] "Anxiously looking at all the new bakers and not wanting any of them to go. #GBBO https://t.co/O2YguY0MWm"                                                                                            
 [46] "Oh dear. #GBBO https://t.co/4FS3X12AOr"                                                                                                                                                              
 [47] "1. Mint Choc Chip is the entry level ice cream for 93% of the United Kingdom. But please don't Google that.\n\n2. \"MagPrue\" can only lead to conspiracy theories.\n\n#GBBO https://t.co/95cC26eXOZ"
 [48] "Please do not try this at home. #GBBO https://t.co/iGwsPuwZBG"                                                                                                                                       
 [49] "Just to clear up the confusion about Prue and Maggie... #GBBO https://t.co/qmm6CGIHS6"                                                                                                               
 [50] "@laurajaycrochet *snaps pencil* #GBBO"                                                                                                                                                               
 [51] "Mint Choc Chip Ice Cream? #GBBO"                                                                                                                                                                     
 [52] "CONTROVERSIAL!!! #GBBO https://t.co/OBHidOZCRz"                                                                                                                                                      
 [53] "It certainly was a kind of magic. #GBBO https://t.co/FytDtVF6ZT"                                                                                                                                     
 [54] "@randallwrites Not in line with the currant climate of acceptance for all is it?"                                                                                                                    
 [55] "Due to popular demand… we’ve giffed up THAT Prue and Paul moment. #GBBO https://t.co/3o02vFa7tU"                                                                                                     
 [56] "Relatable. #GBBO https://t.co/8j2aIL148h"                                                                                                                                                            
 [57] "\"Like a bake out of hell, I'll be scone 'til the mornin' comes...\" #GBBO https://t.co/ExtMCyV1nr"                                                                                                  
 [58] "BREAKING NEWS: Violence has broken out in the Bake Off Tent! #GBBO https://t.co/p9hHrFZqJh"                                                                                                          
 [59] "Every day. #GBBO https://t.co/uFgmRbi9Vf"                                                                                                                                                            
 [60] "Now this would really be going for The Treble. #GBBO https://t.co/A0g0KtvWIv"                                                                                                                        
 [61] "Film. One word. One syllable. #GBBO https://t.co/J4ooIe5Z7u"                                                                                                                                         
 [62] "🎵 IS IT MY IMAGINASHEEUNNN 🎵 #GBBO https://t.co/CKlKBiiEzQ"                                                                                                                                        
 [63] "If you stare at this malt loaf for more than 15 seconds you start to see a Shetland Pony. #GBBO https://t.co/RpA1GSTVaC"                                                                             
 [64] "Prue’s not so sure many of you know what a malt loaf is, especially the youngsters. Time to see if she’s right!\nRT if you know malt loaf\n❤️ if you don’t\n#GBBO https://t.co/VVyDtnxnW2"            
 [65] "This Twitter feed is going to turn into a fan account for Giuseppe’s eyebrows. #GBBO https://t.co/IQTjyrJxBV"                                                                                        
 [66] "😮😮😮 #GBBO https://t.co/0nn2bDErPU"                                                                                                                                                                
 [67] "@carol_ann54 We do seriously respect your integrity for sticking with the healthy option. #GBBO https://t.co/S3aLrdef5H"                                                                             
 [68] "Lizzie should have claimed her mini rolls were inspired by Salvador Dali. #GBBO https://t.co/elx0ZtROrC"                                                                                             
 [69] "How to sum up your feelings on modern life. #GBBO https://t.co/ITY4oIydZ3"                                                                                                                           
 [70] "There are self checkouts in supermarkets. Why not self judging on Bake Off? #GBBO https://t.co/DSbB48CnGt"                                                                                           
 [71] "“They’re a little bit drunk”\n- Amanda #GBBO https://t.co/GjsIAlSN94"                                                                                                                                
 [72] "Don’t blink when you’re near these mini rolls!! 😱😱 #GBBO https://t.co/C2NdpVtPDi"                                                                                                                  
 [73] "It’s important to cover your crack in the Bake Off Tent. #GBBO https://t.co/9tq9Zc6hlF"                                                                                                              
 [74] "@beckythomas_ We're happy that you're happy! #GBBO"                                                                                                                                                  
 [75] "@Emma_Parkin Fine work. #GBBO https://t.co/QrKF801UyO"                                                                                                                                               
 [76] "When the Bake Off ad break hits and the kitchen snack hunt begins. #GBBO https://t.co/TyipjxYVfK"                                                                                                    
 [77] "The poor Bake Off Carpet didn’t even make it to the first ad break of the series without being ruined. 😢😢😢 #GBBO https://t.co/7YDrLkV4Gg"                                                         
 [78] "@Toxicolly Thankin' you! #GBBO"                                                                                                                                                                      
 [79] "George is us at any point over the last 18 months. #GBBO https://t.co/dSyPEfLIhl"                                                                                                                    
 [80] "Say hello to the world’s happiest sponge. 😊 #GBBO https://t.co/ApUAiNuIjC"                                                                                                                          
 [81] "Jürgen’s milk shake brings all the cakes to the tent. #GBBO https://t.co/FynJ114fDc"                                                                                                                 
 [82] "Hands up if you’re a lockdown baker like Chigs! #GBBO https://t.co/6kbQp3RZEb"                                                                                                                       
 [83] "“It is I, Leclerc!”\n#GBBO #AlloAllo https://t.co/bUTCp9Klnh"                                                                                                                                        
 [84] "🙂🙂🙂🙂🙂🙂 #GBBO https://t.co/7FsNfsEQ7c"                                                                                                                                                          
 [85] "Cannot be unseen. #GBBO https://t.co/fzVEeL9ok4"                                                                                                                                                     
 [86] "Mullets are the best medicine. #GBBO https://t.co/msM8iLSTYa"                                                                                                                                        
 [87] "Baking life. #GBBO https://t.co/fyAOe4mZXw"                                                                                                                                                          
 [88] "It’s all about to kick off in the Bake Off Tent! #GBBO https://t.co/3rAjUh0IcK"                                                                                                                      
 [89] "@CabinClassBoy We would but Pet Bunny's agent is a NIGHTMARE to negotiate with... #GBBO"                                                                                                             
 [90] "Same, Maggie. Same. #GBBO https://t.co/GzSHtvu989"                                                                                                                                                   
 [91] "@TheMizzCCB \"Mini Rolls pack a punch\" according to Paul. We've played Chocolate Chili Pepper Roulette before but this is taking it a bit far... #GBBO"                                             
 [92] "Spinoff TV series pitch: Matt Lucas sings well-known TV theme songs… in German… with Jürgen giving his honest critique afterwards. Slide into our DMs if interested, @Channel4. #GBBO"               
 [93] "Life over the past 18 months. #GBBO https://t.co/51ZWaE6Qgp"                                                                                                                                         
 [94] "George is already way too relatable. #GBBO https://t.co/IN5sxHwEDy"                                                                                                                                  
 [95] "Well said, Maggie! #GBBO https://t.co/sGtO6c7G2g"                                                                                                                                                    
 [96] "Spoilers, Noel! Spoilers! #GBBO https://t.co/BTl31GkTh4"                                                                                                                                             
 [97] "@nickopotamus We are pre-watershed, you know. #GBBO"                                                                                                                                                 
 [98] "@S_M_A_B_S Imagine waking up with a mullet too... #GBBO"                                                                                                                                             
 [99] "The bakers have arrived! Here’s a handy guide to who is who in the Bake Off Tent 2021. #GBBO https://t.co/wFKbn3IOFt"                                                                                
[100] "To be revisited in your dreams/nightmares later. #GBBO https://t.co/Oe713kNwTr"                                                                                                                      

Basisfunktionen in stringr

Die Basisfunktionen umfassen