Variables

Variables in R are names used to storage objects that you can manipulate. For instance, let me create a variable “A”, whicha I want it to be the value 1. Basically, in the R console I type:

A=1

then click “Enter”. That command, just created a variable A, which has a value of 1.

To check, type “A” +“Enter”.

A
## [1] 1

Let’s now create a variable called B, which is equal to 2.

B=2

I can now use those two variables to see what happens when I add them:

A+B
## [1] 3

You cal also create a new variable based on other variables. For instance:

C=A-B
C
## [1] -1

You can check all the variables you have created using the command ls()

ls()
##  [1] "A"              "B"              "C"              "f"             
##  [5] "i"              "MeanPopulation" "MeanSample"     "Merge"         
##  [9] "Population"     "r"              "Results"        "Sample"        
## [13] "SampleSize"     "trial"

As you can see, up to this moment I have created three variables, named A, B and C.

With rare exceptions, each line of command in R is independent.

You should try now. Open R in your computer or use the console below. The console below is exactly what you have in R in your computer. We will use this type of window in a few instances in this book, but I recommend you do your practices in your own computer.

Create a variable called D=10 and other called z=5, and calculate thhe difference between D and z.