美文网首页
[19] 《R数据科学》mutate()习题

[19] 《R数据科学》mutate()习题

作者: 灰常不错 | 来源:发表于2020-11-05 23:35 被阅读0次

(1)虽然现在的dep_time和schrd_dep_time变量方便阅读,但不适合计算,因为他们实际上不是连续性数值。将他们转换成一种更方便的表示形式,即从午夜开始的分钟数。

library(dplyr)
library(nycflights13)
transmute(flights, dep_time, sched_dep_time, dep_minutes = dep_time%%100 + dep_time%/%100 * 60,sched_minutes = sched_dep_time%%100 + sched_dep_time%/%100 * 60)
# A tibble: 336,776 x 4
   dep_time sched_dep_time dep_minutes sched_minutes
      <int>          <int>       <dbl>         <dbl>
 1      517            515         317           315
 2      533            529         333           329
 3      542            540         342           340
 4      544            545         344           345
 5      554            600         354           360
 6      554            558         354           358
 7      555            600         355           360
 8      557            600         357           360
 9      557            600         357           360
10      558            600         358           360
# ... with 336,766 more rows

(2)比较 air_time 和 arr_time – dep_time。你期望看到什么?实际又看到了什么?如何解
决这个问题?

transmute(flights, air_time, var = arr_time - dep_time)

(3)比较 dep_time、 sched_dep_time 和 dep_delay。你期望这 3 个数值之间具有何种关系?
dep_time + dep_delay = sched_dep_time
(4)使用排秩函数找出 10 个延误时间最长的航班。如何处理名次相同的情况?仔细阅读
min_rank() 的帮助文件。

arrange(transmute(flights,dep_rank=min_rank(desc(dep_time)),10))
# A tibble: 336,776 x 2
   dep_rank  `10`
      <int> <dbl>
 1   326830    10
 2   326572    10
 3   325966    10
 4   325830    10
 5   323564    10
 6   323564    10
 7   322730    10
 8   321113    10
 9   321113    10
10   320420    10
# ... with 336,766 more rows

(5)1:3 + 1:10 会返回什么?为什么?

1:10
#[1]  1  2  3  4  5  6  7  8  9 10
1:3
#[1] 1 2 3
1:3+1:10
 #[1]  2  4  6  5  7  9  8 10 12 11

遵循循环法则,自动补齐
(6)R 提供了哪些三角函数?

cos(x)
sin(x)
tan(x)
acos(x)
asin(x)
atan(x)
atan2(y, x)
cospi(x)
sinpi(x)
tanpi(x)

相关文章

网友评论

      本文标题:[19] 《R数据科学》mutate()习题

      本文链接:https://www.haomeiwen.com/subject/bqlzvktx.html