R sin() Function - Trigonometric Calculation
R provides a complete set of trigonometric functions: sin(), cos(), tan(), and their inverse functions.
These functions use radians by default and are frequently used in scientific computing and signal processing.
The syntax of trigonometric functions is as follows:
sin(x) cos(x) tan(x) asin(x) # 反正弦 acos(x) # 反余弦 atan(x) # 反正切
Parameter description:
xInput radian value (radians).
Example
# Trigonometric values of common angles
angle_deg <- c(0, 30, 45, 60, 90)
# Convert degrees to radians
angle_rad <- angle_deg * pi / 180
# Calculate sine value
print("sin value:")
print(round(sin(angle_rad), 4))
# Calculate cosine value
print("cos value:")
print(round(cos(angle_rad), 4))
# Calculate tangent value
print("tan value (note infinity at 90 degrees):")
print(round(tan(angle_rad), 4))
angle_deg <- c(0, 30, 45, 60, 90)
# Convert degrees to radians
angle_rad <- angle_deg * pi / 180
# Calculate sine value
print("sin value:")
print(round(sin(angle_rad), 4))
# Calculate cosine value
print("cos value:")
print(round(cos(angle_rad), 4))
# Calculate tangent value
print("tan value (note infinity at 90 degrees):")
print(round(tan(angle_rad), 4))
Executing the above code produces the following result:
[1] "sin 值:" [1] 0.0000 0.5000 0.7071 0.8660 1.0000 [1] "cos 值:" [1] 1.0000 0.8660 0.7071 0.5000 0.0000 [1] "tan 值(注意 90 度无穷大):" [1] 0.0000 0.5774 1.0000 1.7321 Inf
The arctangent function atan2() can be used to calculate the polar angle of a point (x, y) on a plane:
Example
# Calculate the polar angle of point (1,1) (should be 45 degrees)
angle_rad <- atan2(1, 1)
angle_deg <- angle_rad * 180 / pi
print(paste("Angle of point (1,1):", angle_deg, "degrees"))
# Calculate the polar angle of point (-1, 1) (should be 135 degrees)
angle_rad <- atan2(1, -1)
angle_deg <- angle_rad * 180 / pi
print(paste("Angle of point (-1,1):", angle_deg, "degrees"))
angle_rad <- atan2(1, 1)
angle_deg <- angle_rad * 180 / pi
print(paste("Angle of point (1,1):", angle_deg, "degrees"))
# Calculate the polar angle of point (-1, 1) (should be 135 degrees)
angle_rad <- atan2(1, -1)
angle_deg <- angle_rad * 180 / pi
print(paste("Angle of point (-1,1):", angle_deg, "degrees"))
Executing the above code produces the following result:
[1] "点(1,1)的角度: 45 度" [1] "点(-1,1)的角度: 135 度"Other Extensions
R Language Examples