Miscellaneous R functions, mostly for data processing