R: some functions and codes