 # How To Use The Square Function In R There is no built-in square function in R for the squaring operation. However, you can use two operators to calculate the square of a value: * and ^. The below examples will show you how to use them.

## Square Function In R

### With The * Operator

In R, * is an arithmetic operator for multiplication. You can use it to multiply a number by itself. This operator is valid for all number variables in R.

Example

a <- 5
a * a

 25

In this example, we have used the * operator to square the variable a. Since a is an integer, the returned value is also a perfect square. But you can also apply this operator to non-integer numbers as well:

b <- 3.14
b * b

 9.8596

Keep in mind that you shouldn’t use the* operator for non-numeric values. Otherwise, your operation will result in errors. In the command below, R couldn’t process your square operation and the character variable:

string <- "LearnShareIT"
string * string

Error in string * string : non-numeric argument to binary operator

In addition to individual values, you can also square other data structures in R.

Suppose you have a numeric vector of 5 numbers. The * operator will return a vector of the same size whose elements are the square of each element in the original vector.

c <- c(5, 10, 2.5, -8, 0.1)
c * c

 25.00 100.00 6.25 64.00 0.01

It works in a similar way with data frames, meaning you will also get a data frame with the same shape that contains the squares of all elements in the input data frame.

To demonstrate this, let’s create a simple data frame first:

df <- data.frame(
a = 1:4,
b = 5:8,
c = 9:12)


Now check out its content and square it with the * operator:

> df

  a b c
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12

> df * df

   a b c
1 1 25 81
2 4 36 100
3 9 49 121
4 16 64 144

It is important to note that you can’t square a list like that, even though this operation is valid with vectors and data frames. Any attempt to use the * operator to multiply a list by itself would result in errors:

d <- list(5, 10, 2.5, -8, 0.1)
d * d

Error in d * d : non-numeric argument to binary operator

To work around this, you can use the sapply() function. It can help you apply a function over a list.

sapply(d, function(x) x * x)

 25.00 100.00 6.25 64.00 0.01

The sapply() function can actually be used for other data structures like vectors and data frames as well, even though the * operators are still the easier solution.

### With The ^ Operator

Instead of multiplying a value by itself, you can raise it to power 2 with the exponentiation operator (^). You can just use it in place of the * operator in the above examples, including in the sapply() function:

a ^ 2

 25
b ^ 2

 9.8596
c ^ 2

 25.00 100.00 6.25 64.00 0.01
df ^ 2

   a b c
1 1 25 81
2 4 36 100
3 9 49 121
4 16 64 144
sapply(d, function(x) x ^ 2)

 25.00 100.00 6.25 64.00 0.01

## Summary

There isn’t a square function in R for this operation, but you can use either the * or ^ operator. Functions like sapply() can allow you to square individual elements in complex data structures as well.

Maybe you are interested:

Posted in R