Assigning variables

R uses objects. You must assign them using the assignment operator, <-, in order to store them in memory. You can then call an object by typing its assigned name or using print().

Store the value '2' to the object 'x'

x <- 2
x <- 2
print(x)


You can now perform an operation on 'x'.

x + 4


Try one of your own


# Type in the variable 'x' and then add, subtract, multiple or divide it by 
# another number

If you assign another value to 'x', then the new value replaces the old one. Set 'x' to 30 and calculate the square root.


# Remember that ```<-``` is the assignment operator  
# Use ```sqrt()``` to calculate the square root
x <- 30
sqrt(x)

You can assign almost anything to an object. In the next example, assign some values to the objects I have given you and then use them to calculate a z-score.

# enter values
my_score  <- 
class_avg <- 
class_sd  <- 

# calculate z-score (score - mean / sd)
my_z_score <- 

# print value
print(my_z_score)
# enter values
my_score  <- 52.7
class_avg <- 48
class_sd  <- 10
# enter values
my_score  <- 52.7
class_avg <- 48
class_sd  <- 10

# calculate z-score (score - mean / sd)
my_z_score <- (my_score - class_avg) / class_sd

# print value
print(my_z_score)


jvcasillas/ds4ling documentation built on March 4, 2025, 11:18 p.m.