Test data and method for determining it is located at: Use of IF function in R
I need to enter the ‘Test’ data into another function. The data for x and y is provided below (essentially two options, 1 = (89.3, 12.1) and 2 = (97.2, 8.5). The equation below is part of much larger function but this is where the problem is. What i want to do is to call each of the values indiviudally from the vector we have (Test) and for the output to be have 2 values for each value in test. When i link Test in directly i receive a new vector with 50 output values which makes no real sense. If i put the first value from the Test vector in, i receive an output of 2 values which is correct (i have checked this manually).
set.seed(1)
Test <- ifelse(runif(50, 0, 1) < 0.69, rnorm(50, 25, 4), rnorm(50, 28, 4.3))
x <- c(89.3, 97.2)
y <- c(12.1, 8.5)
c(-0.75*(((Test)-x)/y)^2)
output is currently this from the above function:
[1] -21.32750 -55.05068 -25.23216 -49.25087 -20.09236 -53.28421 -19.61593
[8] -51.81307 -21.32136 -62.68412 -22.28700 -56.50245 -21.33593 -47.71792
[15] -18.53459 -55.10348 -16.12545 -42.79888 -19.73790 -40.38937 -17.77048
[22] -51.94862 -19.20262 -54.78823 -18.92120 -51.75187 -22.82277 -52.08655
[29] -18.82475 -45.86141 -16.28231 -56.33673 -24.02006 -50.75048 -14.97440
[36] -40.67076 -20.51602 -50.05597 -21.04648 -58.66084 -20.32455 -65.47588
[43] -20.01028 -53.19754 -15.84125 -52.88386 -23.09141 -50.51265 -19.73710
[50] -50.32994
but taking the first value from test and putting it in manually gives this:
c(-0.75*(((24.77549)-x)/y)^2)
[1] -21.32750 -54.44958
which is what i want for all 50 values in test
Thanks for any help.
Use sapply:
resultis then a 2×50 matrix: