I want help to create a function that does the following operations:
division
r_n
of the variable dT$division
the result of the division of the value of the corresponding row r_n
of dT$result1
by
the value of each row of dT$result1
for dt$treat == A
operation
operation
execute
dT$operation = (2*(dT$division-1))/(2*(dT$division-1)+1)
Where n
in r_n
is the number of each row in the data frame, and takes values from 1 to N
, and N is the total number of observations or rows in the data frame.
#sample data
(note: edited to add the variable id
which is vital to create the desired function)
dT = read.table(header = TRUE, text = "
id group treat result1
1 0 A 0.1438
2 0 A 0.237
3 0 B 0.2774
4 0 B 0.2774
5 0 B 0.2093
6 0 C 0.1206
7 0 C 0.1707
8 0 C 0.0699
9 0 C 0.1351
10 1 C 0.1206
11 1 A 0.1438
12 1 A 0.237
13 1 B 0.2774
14 1 B 0.2774
15 1 B 0.2093
16 1 C 0.1206
17 1 C 0.1707
18 1 C 0.0699
19 1 C 0.1351
20 1 C 0.1206")
#head output
(note: this is a random sample of rows from the original data chosen to facilitate the illustration of the concept)
id group treat result1
1 0 A 0.014
2 0 A 0.02
3 0 B 0.20
4 1 A 0.14
5 1 B 0.27
#expected output
(note: for dT$treat.denominator A2 and A3, this example only shows the visual - because I needed to post the expected output as soon as possible; that means that I calculated the actual values of the variables dT$division and dT$operation only for dT$treat.denominator A1)
id group treat result1 treat.numerator treat.denominator division operation
1 0 A 0.01 A1 A1 1.00 0.00
2 0 A 0.02 A2 A1 1.64 0.56
3 0 B 0.20 B3 A1 20.00 0.97
4 1 A 0.14 A4 A1 14.00 0.96
5 1 B 0.27 B5 A1 27.00 0.98
1 0 A 0.01 A1 A2 1.00 0.00
2 0 A 0.02 A2 A2 1.64 0.56
3 0 B 0.20 B3 A2 20.00 0.97
4 1 A 0.14 A4 A2 14.00 0.96
5 1 B 0.27 B5 A2 27.00 0.98
1 0 A 0.01 A1 A3 1.00 0.00
2 0 A 0.02 A2 A3 1.64 0.56
3 0 B 0.20 B3 A3 20.00 0.97
4 1 A 0.14 A4 A3 14.00 0.96
5 1 B 0.27 B5 A3 27.00 0.98
Thank you in advance for any help.
Answer to Steps 1 and 2
division<-dT$result1/dT$result1[dT$treat=="A"]
dT<-cbind(dT,division)
create a new variable named operation
for each row of the variable operation execute dT$operation = (2*(dT$division-1))/(2*(dT$division-1)+1)
Answers to 3 and 4
dT$operation<-(2*(dT$division-1))/(2*(dT$division-1)+1)