I have a dataframe, df, with a list / vector of numbers written for each object for two repetitions of a test item.
subj item rep vec
s1 1 1 [2,1,4,5,8,4,7]
s1 1 2 [1,1,3,4,7,5,3]
s1 2 1 [6,5,4,1,2,5,5]
s1 2 2 [4,4,4,0,1,4,3]
s2 1 1 [4,6,8,7,7,5,8]
s2 1 2 [2,5,4,5,8,1,4]
s2 2 1 [9,3,2,6,6,8,5]
s2 2 2 [7,1,2,3,2,7,3]
For each element, I want to find the 50% average value of rep 1, and then replace the lowest numbers in the vector rep 2 with 0 until the average value of rep2 becomes less than or equal to the average value of rep1. For example, for s1 item1:
mean(c(2,1,4,5,8,4,7))*0.5 = 2.1
mean(c(1,1,3,4,7,5,3)) = 3.4
mean(c(0,0,0,0,7,5,0)) = 1.7
After deleting the lowest numbers in the rep 2 vector, I want to correct the rep1 and rep2 vectors and perform some other auxiliary arithmetic functions and attach the results to another (long initialized) data frame. At the moment, I am doing this with loops similar to this pseudocode:
for subj in subjs:
for item in items:
while mean(rep2) > mean(rep1)*0.5:
rep2 = replace(lowest(rep2),0)
newDataFrame[i] = correl(rep1,rep2)
; R, /, , ? ?
:
>dput(df)
>structure(list(subj = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("s1", "s2"), class = "factor"), item = c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), rep = c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), vec = list(c(2, 1, 4, 5, 8, 4, 7), c(1, 1, 3, 4, 7,
5, 3), c(6, 5, 4, 1, 2, 5, 5), c(4, 4, 4, 0, 1, 4, 3), c(4, 6,
8, 7, 7, 5, 8), c(2, 5, 4, 5, 8, 1, 4), c(9, 3, 2, 6, 6, 8, 5
), c(7, 1, 2, 3, 2, 7, 3))), .Names = c("subj", "item", "rep",
"vec"), row.names = c(NA, -8L), class = "data.frame")
, dataframe ( rep1 rep2 rep1 vs rep2).
subj item origCorrel newCorrel
s1 1 .80 .51
s1 2 .93 .34
s2 1 .56 .40
s2 2 .86 .79