R.P. Tamil Selvan R.P. Tamil Selvan - 5 months ago 23
R Question

extracting value of variable from dataframe

I have one issue in selecting a value of one variable conditional on the value of another variable in a dataframe.

Dilutionfactor=c(1,3,9,27,80)
Log10Dilutionfactor=log10(Dilutionfactor)
Protection=c(100,81.25,40,10.52,0)
RM=as.data.frame(cbind(Dilutionfactor,Log10Dilutionfactor,Protection))


Now i want to know the value of Log10Dilutionfactor condition on the value of Protection is equal to either 50 (if it appear) or the value immediately just below 50.
when i used
subset(RM,Protection<= 50)
it gives three rows and when I tried
RM[grepl(RM$Protection<=50,Log10Dilutionfactor),]
it gives 0 values with warning message. I really appreciate if someone help me.

Answer Source

You can use 2 subset:

subset(RM,Protection==max(subset(RM,Protection<= 50)$Protection))$Log10Dilutionfactor
# [1] 0.954243