Evaluate code within a function call in R (Use ICC::ICCbare within a loop) -
i want utilize icc::iccbare function within loop. however, iccbare uses concrete variable names input, e.g.:
iccbare(x = group, y = variable1, info = dat) whereby both "group" , "variable1" columns of data.frame "dat" (i.e., dat$variable1); iccbarecannot used y = dat[, i].
in order programme loop hence need evaluate r code within function phone call of iccbare. thought following:
for(i in 1:10){ iccbare(group, names(dat)[i], info = dat) } however, not work. next error printed:
error in '[.data.frame`(data, yc) : undefined columns selected' is there way evaluate statement names(dat)[i]) first before passed function call?
here minimum working illustration problem:
# create info set dat <- data.frame(group=c(rep("a",5), rep("b",5)), variable1=1:10, variable2=rnorm(10)) # loop (i in names(dat)[2:3]){ iccbare("group", i, info = dat) }
i agree @agstudy. bad illustration of non-standard evaluation. can utilize workaround:
v <- "variable1" iccbare("group", v, info = dat) #error in `[.data.frame`(data, yc) : undefined columns selected eval(bquote(iccbare("group", .(v), info = dat))) #$icc #[1] 0.8275862 r
No comments:
Post a Comment