Implemented everything used by R in scala