I am attempting to run a function (a z-test) over a dataframe by first defining the function and then in a seperate step apply it iteratively across all rows. The goal is to form new colunms that include the p-value of that z-test, for the given target-variables. I can't seem to get the iteration to work as I keep getting this unfamiliar error message.
I define the function:
functionname <- function(V1,V2,V3,V4){...}
This step seems to work, I can input values and am getting a correct result. My code for the iteration looks as follows:
for (i in rownames(df)){
df[i, "newvariableA"] <- functionname(df[i,"V1"],df[i,"V2"],df[i,"V3"],df[i,"V4"])
df[i, "newvariableB"] <- functionname(df[i,"V1"],df[i,"V2"],df[i,"V3"],df[i,"V4"])
}
Which produces the erorr message
Error in x > n : comparison of these types is not implemented
I hope you guys could give me a pointer as to why this error is produced or to an alternative approach.
Thanks!
Edit: More info on the function output:
functionname <- function(V1,V2,V3,V4){
ges1 <- V1/sqrt(2)
ges2 <- V2/sqrt(2)
t1 <- (V3/100)*ges1
t2 <- (V4/100)*ges2
sig <- prop.test(c(t1,t2),c(ges1,ges2),correct=FALSE)$p.value
sig <- round(sig,4)
if (sig>0.05){
sig <- "ns"
} else if (sig==0){
sig <- ".0000"
} else{
sig <- substr(toString(sig),start=2,stop=100)
}
return(sig)
}
CodePudding user response:
This was a problem that I saw long ago when I got a class of R few years back and there wasn't any solution to this at that time. so we used DF as a tibble rather than as a dataframe then problem was gone I didn't get it how that fixed the problem but it did if you don't have any constraints to use df as a dataframe you can try it and use as tibble
CodePudding user response:
Use this
for (i in 1:nrow(df)){
df[i,"newvariableA"] <- ztestd(df[i,"V1"],df[i,"V2"],df[i,"V3"],df[i,"V4"])
}