Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
math
cosine
communitycreator

What is cos() in R?

harris910

The cos() function in R calculates the cosine of a number in radians.

The following illustration shows the mathematical representation of the cos() function.

Mathematical representation of the cosine function

Note: This cos() function only works for right-angled triangles.

Syntax

cos(num)

Parameter

This function requires a number that represents an angle in radians as a parameter.

To convert degrees to radians, use the following formula.

radians = degrees * ( pi / 180.0 )

Return value

cos() returns the cosine of a number that is sent as a parameter. The return value is in the range [-1,1].

Code

#positive number in radians
a <- cos(2.3);
print(paste0("cos(2.3): ", a))

#negative number in radians
b <- cos(-2.3);
print(paste0("cos(-2.3): ", b))

#converting the degrees angle into radians and then applying cos()
#degrees = 180
c <- cos(180 * (pi / (180)));
print(paste0("cos(180 * (pi / (180))): ", c))

RELATED TAGS

r
math
cosine
communitycreator
RELATED COURSES

View all Courses

Keep Exploring