https://r4ds.had.co.nz/ reisijad=read_csv("http://www.tlu.ee/~jaagup/andmed/muu/estonia_reisijad.txt") select(reisijad, Country, Sex) colnames(reisijad) select(reisijad, Firstname:Age) select(reisijad, -(Firstname:Age)) select(reisijad, starts_with("C")) select(reisijad, ends_with("name")) select(reisijad, contains("a")) select(reisijad, matches("t[aeiou]")) select(reisijad, matches("[aeiou]$")) select(reisijad, matches("[aeiou]$"), everything()) library(nycflights13) flights colnames(flights) select(flights, contains("dep"), contains("arr"), -contains("sched")) select(flights, dep_time, dep_time) transmute(flights, paev=paste(year,month, day, sep = "-"), origin) reisijad %>% arrange(desc(Age)) %>% transmute(keskvanus=cummean(Age), Age, vahe=Age-.$Age[row_number()+1], vahevanimaga=max(Age)-Age) # Kuvage juurde vahe vanimaga reisijad %>% group_by(Country) %>% summarise(kesk=mean(Age)) reisijad %>% group_by(Country) %>% summarise(kesk=mean(Age), nkesk=mean(Age[Sex=='F'])) # Ridadeks on sood # Tulpadeks on Estonia, Finland ja Sweden # Lahtrite väärtusteks on keskmised vanused reisijad %>% group_by(Sex) %>% summarise(ekesk=mean(Age[Country=='Estonia']), rkesk=mean(Age[Country=='Sweden'])) reisijad %>% filter(Country %in% c("Estonia", "Sweden")) %>% group_by(Country, Sex) %>% summarise(kesk=mean(Age)) %>% spread(Country, kesk) reisijad %>% group_by(Country) %>% filter(n()>10) %>% ungroup() reisijad %>% group_by(Country) %>% filter(n()>10) %>% group_by(Country, Sex) %>% summarise(kesk=mean(Age)) %>% spread(Country, kesk) reisijad %>% group_by(Country) %>% filter(n()>2) %>% arrange(desc(Age)) %>% mutate(nr=row_number()) %>% filter(nr<=3) %>% select(Country, Age, nr) %>% spread(Country, Age) reisijad %>% group_by(Country) %>% filter(Survived==1) %>% filter(n()>2) %>% arrange(Age) %>% mutate(nr=row_number()) %>% filter(nr<=3) %>% select(Country, Age, nr) %>% spread(Country, Age) reisijad %>% group_by(Country) %>% filter(Survived==1) %>% filter(n()>2) %>% arrange(Age) %>% mutate(nr=row_number()) %>% filter(nr<=3) %>% select(Country, Age, nr) %>% spread(Country, Age) %>% write_csv("e:/jaagup/nooremad.csv") nooremad=read_csv("e:/jaagup/nooremad.csv") nooremad isikud=tibble(eesnimi=c("Juku", "Kati", "Mati")) isikud=tibble(eesnimi=c("Juku", "Kati", "Mati"), klass=6) isikud=tibble(eesnimi=c("Juku", "Kati", "Mati"), klass=6, tutvustus=paste(eesnimi, klass, "klassist")) isikud=tibble(eesnimi=c("Juku", "Kati", "Mati", "Madis"), pikkus=runif(length(eesnimi), 150, 170)) isikud sots1=read_delim("http://www.tlu.ee/~jaagup/andmed/sots/ess08_e_1.txt", ";") isikud=tibble(tunnus=c("juku-kalle/m", "kati/n", "madis/m"), vanus=c("12 aastat", "ei tea", "sai just 13")) isikud isikud %>% separate(tunnus, c("eesnimi", "sugu"), sep="/") %>% extract(vanus, "arvuna", "([0-9]+)", remove=FALSE) %>% unite(kood, sugu, arvuna,remove = FALSE)