lag function for data frames

When applying the stats::lag() function to a data frame, you probably expect it will pad the missing time periods with NA, but lag() doesn’t. For example: Nothing happened. Here is an alternative lag function made for this situation. It pads the beginning of the output vecot with as many NA as there are lags, and…

Paired sample t-test in R

Let’s walk through using R and Student’s t-test to compare paired sample data. The book Statistics: The Exploration & Analysis of Data (6th edition, p505) presents the longitudinal study “Bone mass is recovered from lactation to postweaning in adolescent mothers with low calcium intakes”. The total-body bone mineral content (TBBMC) of young mothers was measured…

Basic line chart with ggplot2

ggplot2 is a package for R which easily draws plots that are easier on the eyes than R’s built-in plotting functions, though the grammar is different than what is commonly used in R. This code demonstrates how to prepare a data frame of basic math functions (logarithm, sine, etc.) and plot it with ggplot2. The…

Two browsers for R help documentation

The same excellent documentation for R commands is available through two different help browsers: text and HTML, and let’s see how how each looks, works, and how to switch the default. Look and feel Here is how both look for the same options help page on Windows 7 in Revolution R 4.3: On Linux in…

Delete non-exact duplicates in SAS

When deleting non-complete duplicates in SAS, in each duplicate set you may want to keep a particular record identified by a rule: it may be the oldest, newest, first, or last observation in each set. You need a identifier to be unique, but you can’t randomly choose which observation to keep. To be precise, you…

Sendmail, SMTP, Gmail relay on Fedora 14

I need to send SMTP email out from my home box running Fedora 14, so I can try StatusNet (Laconica microblog). These days email cannot be sent directly out from a residential DSL because it looks like spam. My ISP provides no SMTP relay, but I can use Gmail—thank you, Google. Fedora 14 comes with…