R is generating a warning when i pass character value through functions. Below is an example which results in the warning message "the condition has length > 1". is this a masking issue? I have tried dplyr::case_when() however not getting the correct result. Please provide suggestion for fixing the warning message, for a package.
#data frame with character column titled "Base_Conditions" and some misc. value column "p".
(df=data.frame(
Base_Conditions=LETTERS[1:5],
p=c(1:5)))
myfun is a Function created to apply regression coefficients to a misc. formula.
myfun=function(Base_Condition,Segment){
if(Segment==FALSE) {
if(Base_Condition=="A") {a=-9.86;b=0.79;c=0.49}
if(Base_Condition=="B") {a=-8.56;b=0.60;c=0.61}
if(Base_Condition=="C") {a=-5.13;b=0.60;c=0.20}
if(Base_Condition=="D") {a=-12.526;b=1.204;c=0.236}
if(Base_Condition=="E") {a=-10.008;b=0.848;c=0.448}
if(Base_Condition=="F") {a=-7.182;b=0.722;c=0.337}
a^(2)+b^(3)+log(1/c)} else {
if(Base_Condition=="A") {a=-3;b=0.74;c=0.49}
if(Base_Condition=="B") {a=-83;b=0.60;c=0.61}
if(Base_Condition=="C") {a=-3;b=0.2;c=0.20}
if(Base_Condition=="D") {a=-124;b=1.204;c=0.2}
if(Base_Condition=="E") {a=-10.008;b=0.848;c=0.448}
if(Base_Condition=="F") {a=-7.1;b=0.72;c=0.337}
a^(2)+b^(3)+log(c)
}
}
mainfun is the main function applied to pre defined data frame with "Base Condition" and "p" columns which may be populated with different values for different data sets.
#library(dplyr)
mainfun=function(data,p,Base_Conditions,Segment){
data |> dplyr::mutate( P.T.B=
if(Segment==FALSE) {p*myfun(Base_Condition=Base_Conditions,Segment=FALSE)
} else {
p*myfun(Base_Condition=Base_Conditions,Segment=TRUE)
}
)
}
apply to df.
df |> mainfun(Base_Conditions=Base_Conditions,Segment=FALSE)
outputs
Warning messages:
1: Problem with `mutate()` column `P.T.B`.
ℹ `P.T.B = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
2: Problem with `mutate()` column `P.T.B`.
ℹ `P.T.B = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
3: Problem with `mutate()` column `P.T.B`.
ℹ `P.T.B = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
4: Problem with `mutate()` column `P.T.B`.
ℹ `P.T.B = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
5: Problem with `mutate()` column `P.T.B`.
ℹ `P.T.B = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used
6: Problem with `mutate()` column `P.T.B`.
ℹ `P.T.B = if (...) NULL`.
ℹ the condition has length > 1 and only the first element will be used