i have a dataframe that has 1000 rows and 4 columns where the dataframe has 100 ID's.
the dataframe looks like as follows:
abc <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),
A = c(12,12.5,15,16,18,20,25,26,29,35, 12,12.5,15,16,18,20,25,26,29,35),
B = c(20,19,18,17,16,20,25,28,30,35, 20,19,18,17,16,20,25,28,30,35),
C = c(2,1,5,9,10,11,13,18,25,27,2,1,5,9,10,11,13,18,25,27))
here, the first condition is to select the minimum value from the 'B' column with respect to ID and select the corresponding A th column (i.e. for min(B) = 16, A = 18 for ID-1).
the second condition is to select the minimum value from the 'C' column with respect to ID and select the corresponding A th column (i.e. for min(C) = 1, A = 12.5 for ID-1)
finally, I would like to subset the data frame (from A = 12.5 to A = 18) with respect to ID
the expected/desired output data frame is as follows
abcd <- data.frame(ID = c(1,1,1,1,2,2,2,2),
A = c(12.5,15,16,18,12.5,15,16,18),
B = c(19,18,17,16,19,18,17,16),
C = c(1,5,9,10,1,5,9,10))
i have tried the code as following
library(plyr)
e <- ddply(abc, .(ID), function(z) {
z[z$dmin(abs(z$C)) : min(abs(z$B)), ]
})
but fails to get the desired output