Economics of Money and Banking / Perry G Mehrling / Ders 1

dplyr

library(nycflights13)

library(tidyverse)

nycflights13::flights

filter(flights, month == 1, day == 1)


BOŞ

filter(flights,   is.na(dep_time))

filter(flights,   !is.na(dep_time))


Arrange()


a11 <- arrange(flights,-month)

burada "-" ile "desc" yapılıyor


Select()


select(flights, year, month, day)


select(flights, time_hour, air_time, everything())


burada iki değişkeni başa atıp, diğer hepsini ekliyor.


Mutate()


yeni sütun eklemek için

mutate(flights,

gain = arr_delay - dep_delay,

hours = air_time / 60,

gain_per_hour = gain / hours)


# As well as adding new variables, you can use mutate() to # remove variables and modify existing variables.

starwars %>% select(name, height, mass, homeworld) %>% mutate( mass = NULL, height = height * 0.0328084 # convert to feet)



# Use across() with mutate() to apply a transformation # to multiple columns in a tibble.

starwars %>% select(name, homeworld, species) %>% mutate(across(!name, as.factor))


If you only want to keep the new variables, use transmute():

transmute(flights,

gain = arr_delay - dep_delay,

hours = air_time / 60,

gain_per_hour = gain / hours




transmute(flights,

dep_time,

hour = dep_time %/% 100,

minute = dep_time %% 100

)

#> 517    5     17


 summarize(). It collapses a data frame to a single row:

summarize(flights, delay = mean(dep_delay, na.rm = TRUE))


summarize() is not terribly useful unless we pair it with group_by().


by_day <- group_by(flights, year, month, day)

summarize(by_day, delay = mean(dep_delay, na.rm = TRUE))

Burada her yıl, ay ve gün kombinasyonuna göre dep_delay verisini tek veriye indirgiyor.

EFSANEEEEEE





Yorumlar