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:

xy
A0.1
B0.4
C0.2
D0.3
E0.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.

xyz
A0.10.4
B0.40.4
C0.20.4
D0.30.4
E0.50.4

This can be useful if you want to do scaling based on specific values.

xyzy_scaled
A0.10.40.25
B0.40.41.00
C0.20.40.50
D0.30.40.75
E0.50.41.25