Skip to content

Commit

Permalink
uc2 df rename eg
Browse files Browse the repository at this point in the history
  • Loading branch information
darwinanddavis committed Apr 24, 2024
1 parent 0fc1f46 commit 18e2001
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
10 changes: 7 additions & 3 deletions UsefulCode2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ Rename df with pipe
```{r,d7}
df %>% # df of two cols
`colnames<-`(c("A","B"))
# rename directly with 'select'
df %>%
select(Lat, Lon, "Site" = `Site name`)
```

Split df into multiple dfs based on row value
Expand Down Expand Up @@ -741,22 +745,22 @@ df %>% mutate_at("var1", ~ifelse(. > 0.5, "this","that"))


Gather/melt data into groups/stacks
```{r}
```{r,dplyr14}
df %>% gather(key = "Country", value = "value")
# reorder factors to df
```

Count instances of grouped data
```{r}
```{r,dplyr15}
df %>%
group_by(date) %>%
tally(var1 * var2, name = "new_var1")
```

Lengthen df by column variables
(see https://haswal.github.io/pivot/ for good visual example)
```{r}
```{r,dplyr16}
# select vars you want and lengthen by 'var_n'
# e.g. var_n = year
df %>%
Expand Down
22 changes: 13 additions & 9 deletions UsefulCode2.html

Large diffs are not rendered by default.

0 comments on commit 18e2001

Please sign in to comment.