owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
22 February, 2018
## Rstudio tips & tricks
*feel free to add your Rstudio tips and tricks*
* TAB button to autocomplete
* Ctrl + Alt + I om een nieuwe code chunck toe te voegen in Rmarkdown
* CTRL + Enter voert het commando uit waar de cursor staat. Werkt ook als het commando over meerdere regels staat!
* Ctrl + Shift + Enter voert een volledige chunk in Rmarkdown uit
* ts + TAB -> print tijd en uur
* een aantal cheat sheets zijn beschikbaar in Rstudio, via het menu help -> cheats sheets
* In de console kan je vorige commando's terug oproepen met de pijltjes toetsen
## Moving around and selecting text with the keyboard
- left/right arrow: one character to the left/right
- CTRL + left/right arrow: one **word** to the left/right
- CTRL + SHIFT + left/right arrow: _select_ one **word** to the left/right
## add quotes to make a string
- select (see above) and then click on quotes
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```
libary(tidyverse)
...
```
(*you can copy paste this example and add your code further down, but do not fill in your code in this section*)
Your snippets:
```r
data <- read_csv(...)
```
## Piping
```r
# via piping:
females_weight_above_30 <- data %>%
filter(sex == "F", weight_in_g >30) %>%
select(weight_in_g)
# via nested functions:
females_weight_above_30_2 <- select(filter(data, sex == "F", weight_in_g > 30), weight_in_g)
```
Mutate and save in same df:
```r
# With assigning
df <- df %>% mutate(extra_column = "static value")
# Or
df %>% mutate(extra_column = "static value") -> df
# Shorter way with %<>%, requires library(magrittr)
df %<>% mutate(extra_column = "static value")
```
## Add females_weight_in_g_above_30 as column
With `ifelse()`
```r
dataset %>%
mutate(
females_weight_in_g_above_30 = ifelse(
sex == "F" & weight_in_g > 30,
weight_in_g,
NA
)
)
```
With `case_when()`
```r
dataset %>%
mutate(
females_weight_above_30 = case_when(
weight_in_g > 30 & sex == "F" ~ TRUE
)
)
````
`case_when()` is also useful if you want to do multiple steps:
```r
dataset %>%
mutate(
my_column = case_when(
weight_in_g > 30 & sex == "F" ~ "female above 30",
weight_in_g > 30 & sex == "M" ~ "male above 30",
TRUE ~ "animal below 30"
)
)
```
## Select the 5 lightest animals
```r
Min5 <- survey_data %>% top_n(5, -weight_in_g)
Min5 <- survey_data %>% top_n(-5, weight_in_g)
```
## Ugh, stupid column names in Excel file
Excel might contain column names with capital letters, spaces, etc., which can be annoying to select:
```r
brandganzen <- read_excel("./data/20180123_brandganzen.xlsx")
brandganzen %>% select(`Locatie vangst`) # Ugh
```
With `janitor` your column names can be cleaned (lowercase, underscores instead of spaces). In addition, you can remove empty rows:
```r
library(janitor) # Also tidyverse, but not loaded by default
brandganzen %>%
remove_empty_rows() %>% # Additional step to remove empty rows
clean_names() -> brandganzen
brandganzen %>% select(locatie_vangst)
```
## Filter
```r
# How many adult geese per sex can you count
brandganzen %>%
filter(!is.na(Geslacht)) %>%
filter(Leeftijdscategorie == "Adult") %>%
group_by(Geslacht) %>%
count()
# Or shorter (but less obvious you are grouping):
brandganzen %>%
filter(!is.na(Geslacht)) %>%
filter(Leeftijdscategorie == "Adult") %>%
count(Geslacht)
```
## check working directory or change it
```r
# (get working directory) to know where I am
getwd()
# (set working directory) to go to another directory
setwd() # e.g. setwd("./data/") go in subdirectory
```
# Gent migratie