code from learning R book