1

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.

1
  • 1
    If you cbind before you coerce to a data.frame, you're creating a matrix (which can only hold one type) before changing to data.frame (which can hold multiple). Just use data.frame(...) instead. Commented May 12, 2017 at 5:58

3 Answers 3

1

You can use 2 subset:

subset(RM,Protection==max(subset(RM,Protection<= 50)$Protection))$Log10Dilutionfactor
# [1] 0.954243
Sign up to request clarification or add additional context in comments.

2 Comments

This one is easiest in solving my issue, thanking you
Yes it is not a problem with the data provided, i rechecked it and found correct. that's why i deleted the comment
1

You could use

with(RM, Log10Dilutionfactor[which(Protection == max(Protection[Protection <= 50]))])
# [1] 0.9542425

Comments

1

or find the index value of protection that is closest to 50

 index = which(abs(RM$Protection-50)<=min(abs(RM$Protection-50)))

and then look it up in what ever column you want. e.g for Dilutionfactor

 RM$Dilutionfactor[index]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.