How to convert degree value to cosine in R

Viewed 65

I want to convert my degrees columns (start_time1 and end_time1) into cosines value.

This is what the sample looks like:

# A tibble: 10 x 3
   trip_id start_time1 end_time1
     <int>       <dbl>     <dbl>
 1       1       250.      255. 
 2       2       302.      310. 
 3       3       246.      247. 
 4       4       229.      229. 
 5       5       310.      310. 
 6       6       273.      306. 
 7       7       289.      303. 
 8   94297       111.      117. 
 9   94298       242.      250. 
10   94299        24.1      25.1
data <- structure(list(trip_id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 94297L,94298L, 94299L), start_time1 = c(249.741666666667, 301.666666666667,246.045833333333, 228.933333333333, 309.508333333333, 272.575,288.758333333333, 110.933333333333, 242.366666666667, 24.0666666666667), end_time1 = c(254.741666666667, 310.166666666667, 246.795833333333,229.433333333333, 309.983333333333, 306.429166666667, 302.508333333333,117.183333333333, 249.866666666667, 25.0666666666667)), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame")) 

Are there any packages and functions from R that can compute this?

Thanks in advance for any help.

1 Answers

As Roland mentioned in his comment you can do this (it creates two new columns)

df <- structure(list(trip_id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 94297L,94298L, 94299L), start_time1 = c(249.741666666667, 301.666666666667,246.045833333333, 228.933333333333, 309.508333333333, 272.575,288.758333333333, 110.933333333333, 242.366666666667, 24.0666666666667), end_time1 = c(254.741666666667, 310.166666666667, 246.795833333333,229.433333333333, 309.983333333333, 306.429166666667, 302.508333333333,117.183333333333, 249.866666666667, 25.0666666666667)), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame")) 
df$start_time1_cos= cos(df$start_time1/180*pi)
df$end_time1_cos= cos(df$end_time1/180*pi)

you can keep the same columns names and convert them by doing:

df$start_time1= cos(df$start_time1/180*pi)
df$end_time1= cos(df$end_time1/180*pi)
Related