Writing Functions in R

Examples

Using the reviews_sample.csv file, use the longesttest function to print the longest review in the text column.

longesttest <- function(x) {x[which.max(nchar(x))]}
Click to see solution
beerDF <- read.csv("/anvil/projects/tdm/data/beer/reviews_sample.csv")
longesttest <- function(x) {x[which.max(nchar(x))]}

longesttest(beerDF$text)
 '\302\240\302\240 32 fl oz brown glass Alesmith grumbler filled earlier today at The Growler Room in Austin, Texas, and stored in my fridge until consumption. Served into a Spaten stangekrug. Reviewed at low altitude live here in Austin on 08/31/13. Cost was 21 USD. Expectations are sky high given both its current ratings and the description \342\200\223 which makes it sound tasty. This is my first Texan sour. Served frigid cold and allowed to warm following the initial pour. Side-poured with extra vigor as slight carbonation issues are anticipated given that it\342\200\231s a growler pour. A: No bubble show forms as I pour. Pours a one finger wide head of gorgeous khaki colour. Great creaminess \342\200\223 reminiscent actually of an Irish Dry Stout\342\200\231s head. Good thickness. Supple. Has an appealing soft complexion. Okay lacing; some legs cling to the sides of the glass when I tilt it, but they quickly fall. Retention is pretty good \342\200\223 about 3-4 minutes; not too shabby for a growler pour. Body colour is a nontransparent murky caramel-brown. Some light glowing translucency near the bottom of the glass; it\342\200\231s certainly not opaque. No yeast particles are visible. It\342\200\231s not incredible to look at, but there\342\200\231s nothing overtly wrong with it. Looking forward to trying it. Sm: Tart acetic cherries, cherry pie crust, and accompanying sourness. It\342\200\231s got more of a Flanders Red feel than anything else. Very light funkiness. I\342\200\231m very impressed with the yeast-derived sourness and I\342\200\231m rather curious what strain was used; this doesn\342\200\231t have the sterile clinical feel of a sour brewed purely with lactobacillus (at least based on the aroma) \342\200\223 there\342\200\231s more complexity here, and the combination of well-integrated sourness with a subtle funkiness and complex tart character approximates lambic yeast. The yeast alone elevates the aroma beyond that of most wild ales. Unlike many subpar \342\200\234wild\342\200\235 ales, this one actually smells like it\342\200\231s still alive \342\200\223 and I don\342\200\231t find myself doubting that wild yeast and bacteria were used in fermentation. It stops a bit short of evoking true spontaneous open fermentation, but I\342\200\231m nitpicking. Very buried hints of vanilla and maple; they\342\200\231re by no means overt. Layered caramel and biscuit malts. As it warms, the otherwise reticent oak notes begin to creep out; I don\342\200\231t know that I\342\200\231d say they\342\200\231re evocative of a barrel per se. The cherry notes are dominant, a sound choice. No alcohol is detectable, nor is any significant hop character. A damn fine aroma for an American sour. Mild in strength, with well-executed subtlety. I\342\200\231m impressed. I just hope the taste lives up to the smell. T: Tart cherries, acetic cherry pie crust notes (though not as prominent as I\342\200\231d like), and a pleasant core sourness throughout are the first things I notice. It\342\200\231s got a clean simple open comprised mainly of biscuit malts with a dash of caramalt in there too, a malty combination that continues on through the second act to form the body of the beer. Luckily, the malts don\342\200\231t intrude and the beer\342\200\231s sourness is allowed to take centre stage. Speaking of the sourness, it\342\200\231s by no means puckerworthy. It\342\200\231s somewhat tame yet still satisfying. This would be a good introduction to (American) sours for a beginner. Acidic. I have to retract what I said about the yeast above; it by no means approximates lambic yeast in the actual taste, and does suffer from the same sterile, clean, clinical feel of most American wild ales/sours. Unfortunate. Oak is present as a guiding undertone. Luckily, those vanilla and maple notes from the aroma are near imperceptible. No hop character or alcohol comes through. It\342\200\231s got a good balance to it, and it\342\200\231s nearest a Flanders Red Ale in terms of build. That said, it lacks the complexity of the best beers in that style; in fact, I\342\200\231d say even Rodenbach Classic has a superior build in that respect. There is some nice subtlety here, but ultimately the second act feels a bit empty. More flavour depth and diversity is needed. Flavour duration and intensity are about average. Mf: Smooth and wet. Not oily, hot, gushed, or boozy. Acidic. Pretty good thickness and presence on the palate, if a bit unrefined for the style. Fits the attempted style well, but it doesn\342\200\231t feel custom-tailored to this beer\342\200\231s flavour profile specifically. Decent body. I would by no means call this a crisp beer. It\342\200\231s undercarbonated, but I\342\200\231m confident blaming that on the growler. I find myself tempted to smack my lips a biteen whilst consuming it; I consider that a good thing for a sour. Dr: Solid Flanders Red mimicry from the Yanks at Real Ale. I haven\342\200\231t had the base beer, which I understand is their \342\200\234Real Heavy\342\200\235 scotch ale, but the beer never seems to out its base \342\200\223 which seems favorable for a sour. I wouldn\342\200\231t know the base was a wee heavy if I tried it blind, and I consider that alone a feat. This is ultimately a success from Real Ale, though the high price point as well as the availability of better cheaper offerings in the style will prevent me from buying it again. B- \302\240'
Click to see solution
beerDF <- read.csv("/anvil/projects/tdm/data/beer/reviews_sample.csv")
mostpopulardate = function(x) {tail(sort(table(x)), n=1)}
mostpopulardate(beerDF$date)
x
2011-11-15
      1497