R数据科学

0911 Chapter 3 使用dplyr进行数据转换-p42

2018-09-11  本文已影响0人  森尼啊

1. p34变量类型

int, dbl, chr, dttm, lgl, fctr , date

2. dplyr基础

3.filter()

- 比较运算符

<,<=,>,>=,!=,==

-逻辑运算符

缺失值

用NA表示,可用is.na()确认,具有 可传染性
filter()只能筛选出值为TRUE的行
filter(df,is.na() | x >1)

p38练习题

1.

a. filter(flights, arr_delay >=120)
b. View(flights)
filter(flights, dest == 'IAH' | dest == 'HOU')
c. nycflights13::airlines
filter(flights, carrier %in% c('UA','AA','DL'))
d. summer.flights <- filter(flights, month %in% c(7,8,9) ||答案:filter(flights, month >= 7, month <= 9)
e. filter(flights, dep_delay == 0 & arr_delay >120) || 答案: filter(flights, dep_delay <= 0, arr_delay > 120)
f. 此题理解为出发时间至少延误一小时,但是到达时间延误时间少了半小时 filter(flights, dep_delay >= 60, dep_delay - arr_delay > 30)
g.filter(flights, dep_time >= 0 & dep_time <=6) ❌ ||注意到午夜的特殊性,答案:filter(flights, dep_time <= 600 | dep_time == 2400) 或者 filter(flights, dep_time %% 2400 <= 600)

2.

?between()后,This is a shortcut for x >= left & x <= right, implemented efficiently in C++ for local values, and translated to the appropriate SQL for remote tables.
改写前面的2g答案 filter(flights, between (dep_time, 0,6)) ❌ 答案:filter(flights, between(month, 7, 9))

3.

filter(flights, is.na(dep_time == NA))filter(flights, is.na(dep_time)) 到达时间也有缺失值,应该是取消的航班

4.

所有值的0次方都是1
NA | TRUE, 逻辑或运算,只要有一个值为真,结果即为真
FALSE & NA 逻辑与运算,有一个值为假,结果即为假
for all finite, numeric x,x0 = 0,但NA * 0 不等于0,因为,x * ∞ and x−∞ is undefined. R represents undefined results as NaN, which is an abbreviation of “[not a number]

4. arrange()

常用函数

arrange(flights,year, month,day)
arrange(flights,desc(arr_delay)) # 降序排列

p40 练习题

1.

arrange(flights, desc(is.na(dep_time)), dep_time)

2.

2.

理解为出发时间耽误最长,arrange(flights, desc(dep_time))
2013年1月9日,9:00应该出发的,HA 51, JFK to HNL,延迟了1301分钟。
出发时间最早,arrange(flights, dep_time) || 答案不对吧

3.

速度最快应该是 2013年5月25日17点出发的
arrange(flights, desc(distance/air_time)) ||答案:arrange(flights, distance / air_time * 60)

4.

arrange(flights, desc(air_time)) ||
答案:arrange(flights, desc(distance)),飞行时间最长不应该是 air_time么。

5. select()

找到感兴趣的变量,select(),快速生成一个有用变量子集。

-starts_with("abc"), ends_with("xyz"), contains("ijk"),matches

p42 练习题

1.

variables <- c("dep_time", "dep_delay", "arr_time", "arr_delay")
select(flights, one_of(variables))

select(flights, starts_with("dep_"), starts_with("arr_"))
select(flights, matches("^(dep|arr)_(time|delay)$"))

2.

不会发生什么,select()函数会自动忽略重复
select(flights, year, month, day, year, year)

3.

见上面。

4.

没明白说啥
是说虽然代码是大写,依然选择出了小写的列?

上一篇 下一篇

猜你喜欢

热点阅读