Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Last active March 5, 2023 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyhottommy/c3a40e795cc2667bbeb15f6901cd548a to your computer and use it in GitHub Desktop.
Save crazyhottommy/c3a40e795cc2667bbeb15f6901cd548a to your computer and use it in GitHub Desktop.
R tricks
> library(dplyr)
> cars %>%
as_tibble %>%
mutate(case_code = case_when(
speed == 4 & dist == 2 ~ "this",
dist > 6 & dist == 10 ~ "is",
speed >=10 & dist >= 18 ~ "awesome"))
# A tibble: 50 × 3
speed dist case_code
<dbl> <dbl> <chr>
1 4 2 this
2 4 10 is
3 7 4 NA
4 7 22 NA
5 8 16 NA
6 9 10 is
7 10 18 awesome
8 10 26 awesome
9 10 34 awesome
10 11 17 NA
# … with 40 more rows
# diff minus the previous number in sequence
> a<- c(1,2,5,7,9,14)
> diff(a)
[1] 1 3 2 2 5
## the long way
> a
[1] 1 2 5 7 9 14
> lag(a, 1)
[1] NA 1 2 5 7 9
> a - lag(a,1)
[1] NA 1 3 2 2 5
>lead(a,1)
[1] 2 5 7 9 14 NA
> library(dplyr)
# find the max horse power(hp) by cly(Number of cylinders) and gear group(Number of forward gears):
# use ?mtcars to find more information of this built-in dataset
> mtcars %>% group_by(cyl, gear) %>% top_n(n =1, wt = hp)
# A tibble: 10 × 11
# Groups: cyl, gear [8]
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
2 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
3 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
4 17.8 6 168. 123 3.92 3.44 18.9 1 0 4 4
5 21.5 4 120. 97 3.7 2.46 20.0 1 0 3 1
6 13.3 8 350 245 3.73 3.84 15.4 0 0 3 4
7 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2
8 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6
9 15 8 301 335 3.54 3.57 14.6 0 1 5 8
10 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2
> mtcars %>% group_by(cyl, gear) %>% slice(which.max(hp))
# A tibble: 8 × 11
# Groups: cyl, gear [8]
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21.5 4 120. 97 3.7 2.46 20.0 1 0 3 1
2 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2
3 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
5 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
6 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6
7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
8 15 8 301 335 3.54 3.57 14.6 0 1 5 8
> mtcars %>% group_by(cyl, gear) %>% filter(hp == max(hp))
# A tibble: 10 × 11
# Groups: cyl, gear [8]
mpg cyl disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
2 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
3 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
4 17.8 6 168. 123 3.92 3.44 18.9 1 0 4 4
5 21.5 4 120. 97 3.7 2.46 20.0 1 0 3 1
6 13.3 8 350 245 3.73 3.84 15.4 0 0 3 4
7 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2
8 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6
9 15 8 301 335 3.54 3.57 14.6 0 1 5 8
10 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2
## note the order of the rows are different, but they return the same rows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment