How to Create a Variable in R?

Spread the love

Problem –

You want to save a value in a variable.

Solution –

To save a value in a variable we define the name of the variable followed by the assignment operator (<-) and the value of the variable. The assignment operator is made of a less-than character(<) and a hyphen(-) with no space between them.

Let’s say you want to create two variables and add them together.

> a <- 5
> b <- 10
> c <- a + b
> print(c)
[1] 15

We can also check the value of a variable by typing it in the console.

> a
[1] 5
> b
[1] 10
> c
[1] 15

Rating: 1 out of 5.

Posted in RTagged

Leave a Reply