Useful R snippets
A collection of useful R snippets
Published
Last updated
Create a new column with a value from a specific cell
Take the following simple data frame:
| 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.
| 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.
| 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 |