Randomly selected data matrics and linear mixture in R

37 views Asked by At

My intention is to generate a set of new data from two datasets by different proportions.

Dataset A looks like this

V1              V2           V3
17.73401882 15.57104835 38.38786527
17.46926296 15.51123547 38.15194112
17.7728391  15.57638409 38.40830517
17.73169731 15.56926588 38.38397312
17.60030382 15.53801358 38.26190276
17.7475358  15.57271454 38.41432083
17.83174879 15.59799329 38.38570917
17.81596492 15.58560974 38.42538827
17.81740582 15.58646407 38.42952669
17.47055878 15.51216794 38.15616593
17.6358243  15.54136455 38.31012518
17.80583315 15.5891604  38.4914876
17.63499101 15.54602387 38.24489252
17.6344578  15.54014455 38.30676618
17.81776898 15.5877239  38.43091498
17.77471768 15.57850342 38.40982719
17.63680287 15.54800569 38.2632379
17.71502505 15.56852745 38.35463946
17.46939668 15.51230794 38.15553654
17.35707843 15.48526029 38.05952664
17.59634454 15.53536295 38.25210731
17.71245901 15.56312681 38.33309101
17.57088548 15.55072588 38.34755005
17.52448926 15.52427652 38.21140341
17.45185431 15.50805358 38.13888248
17.52351315 15.52323983 38.2099672
17.92164576 15.60921197 38.54586884
17.97156359 15.6158826  38.56507722
17.92850307 15.60417939 38.55265903
18.0122483  15.61884485 38.5898537

And dataset B is

V1  V2  V3
18.331  15.564  38.206
18.341  15.573  38.234
18.368  15.609  38.355
18.341  15.568  38.209
18.259  15.563  38.417
18.352  15.594  38.308
18.336  15.573  38.237
18.346  15.584  38.277
18.285  15.516  38.046
18.292  15.514  38.035
18.293  15.518  38.054
18.261  15.482  37.935
18.279  15.508  38.026
18.272  15.502  37.999
18.29   15.515  38.012
18.257  15.476  37.916
18.7    15.85   39.01
18.3    15.53   38
18.3    15.5    37.98
18.46   15.72   38.67
18.49   15.76   38.91
18.53   15.78   38.99
18.3    15.52   38.05
18.45   15.67   38.47
18.33   15.56   38.19
18.33   15.56   38.17
18.39   15.63   38.41
18.5    15.73   38.62
18.33   15.56   38.17
18.34   15.59   38.27

I want to randomly select one row of variables (V1,V2,V3) from each dataset and mixing them by different proportions to create a new set of V1, V2,V3. Something like this: C1=10%A+90%B, C2=20%A+80%B,C3=30%A+70%B. I wish to run this sort of selection for 1000 times to create 1000 data matrices for each new dataset under specific mixing ratios. Does anyone know how to do that in R?

1

There are 1 answers

0
R. Schifini On

You can use ifelse like this:

a = data.frame(v1 = rep(15,30),v2 = rep(20,30),v3 = rep(25,30))
b = data.frame(v1 = rep(5,30),v2 = rep(0,30),v3 = rep(-5,30))

c = data.frame(v1 = ifelse(runif(nrow(a)) < 0.1, a$v1, b$v1),
               v2 = ifelse(runif(nrow(a)) < 0.2, a$v2, b$v2),
               v3 = ifelse(runif(nrow(a)) < 0.3, a$v3, b$v3)
)

In this case, runif(nrow(a)) will generate uniform random numbers between 0 and 1 that will help you choose a value from either a or b. The proportions are set inside the ifelse as 0.1, 0.2 and 0.3.