mardi 11 mai 2021

How to build a data frame where one column adds onto the other in R

I'd like to create a randomized data frame in R where the values of the 2nd column equals 1st column plus number and the next value of the 1st column equals 2nd column plus number

It would look something like this:

I've tried doing this:

Sal = rnorm(150,mean=18,sd=1.7)
H2O = Sal + 0.2358
d = data.frame(Sal = rep(Sal,1), H2O = rep(H2O,1))
df = d[order(d$Sal,d$H2O),]
df

But it doesn't really work since the next number doesn't "build" upon the previous number.

How could I do this? Should I use a loop instead? My R experience is fairly limited (as you can probably tell)

Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire