R.P. Tamil Selvan
R.P. Tamil Selvan

Reputation: 43

Apply if function to identify the value of variable based on the value of another variable

I am trying to identify the value of the variable in an R data frame conditioning on the value of another variable, but unable to do it. Basically, I am giving 3 different Dose of vaccine to three groups of animals (5 animal per group ( Total )) and recording results as Protec which means the number of protected animals in each group. From the Protec, I am calculating the proportion of protection (Protec/Total as Prop for each Dose group. For example

library(dplyr)
Dose=c(1,0.25,0.0625);Dose #Dose or Dilution
Protec=c(5,4,3);Protec
Total=c(rep(5,3));Total
df=as.data.frame(cbind(Dose,Protec,Total));df
df=df %>% mutate(Prop=Protec/Total);df   

Question is, what is the log10 of minimum value of Dose for which Prop==1, which can be found using the following code

X0=log10(min(df$Dose[df$Prop1==1.0]));X0

The result should be X0=0

If the Protec=c(5,5,3), the Prop becomes c(1.0,1.0,0.6) then the X0 should be -0.60206. If the Protec=c(5,5,5), the Prop becomes c(1.0,1.0,1.0), For which I want X0=0. if the Protec=c(5,4,5), the Prop becomes c(1.0,0.8,1.0), then also I want X0=0 because I consider them as unordered and take the highest dose for calculating X0

I think it requires if function but the conditions for which I don't know how to write the code.

can someone explain how to do it in R?. thanking you in advance

Upvotes: 0

Views: 45

Answers (1)

akrun
akrun

Reputation: 886938

We can use mutate_at to create apply the calculation on multiple columns that have column name starting with 'Protec'

library(dplyr)
df1 <- df %>%
         mutate_at(vars(starts_with("Protec")), list(Prop = ~./Total))

Upvotes: 1

Related Questions