Expert Programming

Guide to Syntax in R

How a Data Scientist uses R

Vinita Silaparasetty
Jan 17 · 2 min read

Always use <- as the assignment operator.

R is case sensitive.

N/A indicates: Not available

Indeces in R always start with 1.

All strings and characters must be in double quotes. Eg: “Hello” or “a”

When you want to see a value printed on your screen you can just enter the name of the variable to be printed.

a = 20
a

Add comments through out your code to make it easier to understand.

# Performing Addition

You don’t have to keep hitting the run button to run your code, just hit enter.

Use ””” to write multi line comments.

""" This is the first comment.
This is the second comment """

When creating a vector with multiple variables, use the combine operator which is denoted by a c.

n <- c(1,2,3)

my_vector <- c(1, 4, "hello", TRUE)

Watch out for c( )! It can also function as “convert” or “coerce”

My_list <- list(1,4,"hello", TRUE)

You will not have a vector with two integer objects, one character object and one logical object. Instead, c() will do what it can to convert them all into all the same object type, in this case all character objects. So my_vector will contain “1”, “4”, “hello” and “TRUE”.

To create a collection with multiple object types, you need a list, not a vector. Create a list with the list() function.

My_list <- list(1,4,"hello", TRUE)

R does offer for, while and repeat loops, but the commonly used functions are apply() functions.

Null is considered as 0. This means it has no value.

NA indicates a missing value and is picked up as a variable, but without a value

is.na returns boolean values

R chooses the final element to define all elemet types in the vector.

for example (1,2,3,”a”) The class will be returned as character

For scan ( ) function keep entering the desired values and when you are done leave the final slot blank and hit enter, you will be taken back to the concole prompt.

[ ] gives elements in an object.

Read in tabular data from a website:

library(data.table)
mydat <- fread('C://Some/File/Path.csv')

~ is called tilde, in R language it is used to separate the left- and right-hand sides in a model formula.


Connect With Me:

Facebook, Twitter, Quora, Youtube and Linkedin.

Vinita Silaparasetty

Vinita Silaparasetty explains concepts in AI, Data Science, Machine Learning and Deep Learning.

Vinita Silaparasetty

Written by

Vinita Silaparasetty is a data scientist exploring the field of Artificial Intelligence, particularly in Machine Learning, Deep Learning and Neural Networks.

Vinita Silaparasetty

Vinita Silaparasetty explains concepts in AI, Data Science, Machine Learning and Deep Learning.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade