Useful R snippets
A collection of useful R snippets
Published
- Published On
Last updated
- Last Updated
library(tidyverse)
Create a new column with a value from a specific cell
Take the following simple data frame:
df <- tribble(
~x, ~y,
"A", 0.1,
"B", 0.4,
"C", 0.2,
"D", 0.3,
"E", 0.5
)
df
| x | y |
|---|---|
| A | 0.1 |
| B | 0.4 |
| C | 0.2 |
| D | 0.3 |
| E | 0.5 |
Using the following code, we can add a new column that repeats a
specific value from column y based on a value in a column x.
df <- mutate(df, z = nth(y, which(x == "B")))
df
| x | y | z |
|---|---|---|
| A | 0.1 | 0.4 |
| B | 0.4 | 0.4 |
| C | 0.2 | 0.4 |
| D | 0.3 | 0.4 |
| E | 0.5 | 0.4 |
This can be useful if you want to do scaling based on specific values.
df |>
mutate(y_scaled = y / z)
| x | y | z | y_scaled |
|---|---|---|---|
| A | 0.1 | 0.4 | 0.25 |
| B | 0.4 | 0.4 | 1.00 |
| C | 0.2 | 0.4 | 0.50 |
| D | 0.3 | 0.4 | 0.75 |
| E | 0.5 | 0.4 | 1.25 |