Code conversion to R