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…

The debug mode is URL but there is no URL specified

In Microsoft Web Developer 2010 Express I saw this error: The debug mode is URL but there is no URL specified. Please specify a URL using the project’s Debug property page. This happened whenever I tried Start debugging after I changed from the 4.0 runtime to the 2.0 runtime, but switching back to 4.0 didn’t…

Strip Non-Printable ASCII Characters (SAS)

Say in your SAS data set, which comes from a text file, XML, or database, has non-ASCII characters that look like garbage—perhaps an odd square. These are probably control characters that mean something to some system, but they may be useless to you. The good news is they are easy to filter out or to…

Hello World in pycairo

Cairo is a portable 2D graphics library for drawing to PDF, PNG, SVG, X11, Windows, etc. Here is a minimal example (hello world) to create a PNG with text on a solid background written in pycairo (Cairo for Python). This code was tested with PyCairo 1.8, Cairo 1.10, Python 2.7, and Fedora 14, but it…