Cook RR数据科学

0914 Chapter 3 使用dplyr进行数据转换

2018-09-15  本文已影响88人  森尼啊

心好累。。看着和书上的代码一样,不是object not found 就是unexpected symbol等各种error,小白的进化之路还有一段距离啊。。。

mutate()

总是将新列添加在数据集的最后。
mutate(flights_sml, gain = arr_delay - dep_delay, speed = distance / air_time *60 )

mutate(flights_sml, gain = arr_delay - dep_delay, hours = air_time / 60, gainr_per_hour = gain /hours )

常用创建函数

p45练习

df1 <- mutate( flights,
dep_hours = dep_time %/% 100, 
dep_minute = dep_time %% 100)

mutate(flights, 
dep_minutes =  df1$dep_hours * 60 + df1$dep_minute  )
df2 <- mutate( flights,
sched_dep_hours = sched_dep_time %/% 100, 
sched_dep_minute = sched_dep_time %% 100)

mutate(flights, 
sched_dep_minutes =  df2$sched_dep_hours * 60 + df2$sched_dep_minute  )

|| 答案: 考虑物业24:00的特殊情况,2400可能是1440分钟,也可能是0分钟。

flights_times <- mutate(flights,
    dep_time_mins = (dep_time %/% 100 * 60 + dep_time %% 100) %% 1440,
    sched_dep_time_mins = (sched_dep_time %/% 100 * 60 + sched_dep_time %% 100) %% 1440
  )

也可以自定义函数,是代码更简洁

time2mins <- function(x) {
  (x %/% 100 * 60 + x %% 100) %% 1440}

flights_times <- mutate(flights,
       dep_time_mins = time2mins(dep_time),
       sched_dep_time_mins = time2mins(sched_dep_time))

感觉 arr_time - dep_time= air_time的,但是实际上 arr_time - dep_time的值大于air_time的值。经过小时的转化就行。
-答案 :飞机经过午夜的话,arr_time<dep_time,结果为负;飞机经过不同的时区也有影响

flights_airtime <- 
  mutate(flights,
         dep_time_min = (dep_time %/% 100 * 60 + dep_time %% 100) %% 1440,
         arr_time_min = (arr_time %/% 100 * 60 + arr_time %% 100) %% 1440,
         air_time_diff = air_time - arr_time + dep_time)

filter(flights_airtime, air_time_diff %% 60 == 0)

通过以下代码,方便观看

df5 <- select(flights, 
dep_time,
sched_dep_time, 
dep_delay)

dep_delay = dep_time - dep_delay
-答案:全部转成分钟后等式也不完全成立,虽然在这个题目中,时区不影响离开时间,但是可能有航班被安排了跨午夜了,

min_rank(desc(flights$dep_delay))

-答案:如果时间一样,比如三个航班是一样的时间,在第一次标记下,第二次第三次不标记。

flights_delayed <- mutate(flights, dep_delay_rank = min_rank(-dep_delay))
flights_delayed <- filter(flights_delayed, dep_delay_rank <= 20)
arrange(flights_delayed, dep_delay_rank)

-5.
返回 Warning message:
In 1:3 + 1:10 :
longer object length is not a multiple of shorter object length
两个长度不同的向量相加时,会自动循环较短的那个

help('Trig')

R提供了cos(x),sin(x),tan(x),
acos(x),asin(x),atan(x),atan2(y, x),
cospi(x),sinpi(x),tanpi(x)

summarize()

summarize() + group_by()→→分组摘要
na.rm,计算前去除缺失值

常用摘要函数

按多个变量分组

→→ 每次摘要统计都会用掉末尾的变量

取消分组

ungroup

p57练习题

1.

2.

by_dest <- group_by(flights, dest)
summarize(by_dest, count = n())

答案

group_by(dest) %>%
  summarise(n = length(dest))
not_canceled %>%
  group_by(dest) %>%
  summarise(n = n())
not_cancelled %>%
  group_by(tailnum) %>%
  summarise(n = sum(distance))

group_by() + tally() →→→ count()

not_cancelled %>%
  group_by(tailnum) %>%
  tally()

3.

飞机从未起飞,就不会到达;飞机出现空难,能离开不会达到;飞机可能将落到别的地点
更重要的是arr_delay

4. 瞄的答案

canceled_delayed <-
  flights %>%
  mutate(canceled = (is.na(arr_delay) | is.na(dep_delay))) %>%
  group_by(year, month, day) %>%
  summarise(prop_canceled = mean(canceled),
            avg_dep_delay = mean(dep_delay, na.rm = TRUE))

ggplot(canceled_delayed, aes(x = avg_dep_delay, prop_canceled)) +
  geom_point() +
  geom_smooth()

5.

flights %>%
  group_by(carrier) %>%
  summarise(arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
  arrange(desc(arr_delay))

6.?

7.

The sort argument to count() sorts the results in order of n. You could use this anytime you would run count() followed by arrange().

分组新变量(和筛选器)

分组筛选器的作用相当于分组新变量 + 未分组筛选器

p59练习题

剩下的基本都看答案 就不搬运了

1.

会在组内进行操作而不是在整个数据集

2.

flights %>%
group_by(tailnum) %>%
summarise(arr_delay = mean(arr_delay)) %>%
filter(min_rank(desc(arr_delay)) <= 1)

上一篇下一篇

猜你喜欢

热点阅读