代码编写规则

《区域水环境污染数据分析实践》
Data analysis practice of regional water environment pollution

苏命、王为东
中国科学院大学资源与环境学院
中国科学院生态环境研究中心

2024-04-09

tidy data

knitr::include_graphics("../../image/tidy-1.png", dpi = 270)

pipe(管道) |>

require(patchwork)
plot(1:10)
1:10 |> plot()
plot(x = 1:10, y = sin(1:10))
1:10 |> plot(y = sin(1:10))

pipe(管道):%>%

require(magrittr)
1:10 %>% plot()
1:10 %>% plot(y = sin(1:10))
sin(1:10) %>% plot(1:10, .)
sin(1:10) |> plot(x = 1:10, y = _)

代码编写规则

# Strive for:
short_flights <- flights |> filter(air_time < 60)
# Avoid:
SHORTFLIGHTS <- flights |> filter(air_time < 60)

# Strive for
z <- (a + b)^2 / d
# Avoid
z<-( a + b ) ^ 2/d

# Strive for
mean(x, na.rm = TRUE)
# Avoid
mean (x ,na.rm=TRUE)

练习

flights|>filter(dest=="IAH")|>
  group_by(year,month,day)|>summarize(n=n(),
delay=mean(arr_delay,na.rm=TRUE))|>filter(n>10)

练习

flights |>
  filter(dest == "IAH") |>
  group_by(year, month, day) |>
  summarize(n = n(),
    delay = mean(arr_delay, na.rm = TRUE)) |>
  filter(n > 10)

quarto

欢迎讨论!

苏命|https://drwater.rcees.ac.cn; https://drwater.rcees.ac.cn/bcard; Slides