I have a data.frame with 6 columns. The first is for subjects, the second for blocks in an experiment, and columns 3,4 and 5 are values I need to calculate a binary score (0 or 1), that I want to add in the sixth column (that’s why now, it’s full of 0s).
head(kfdblock3to9)
subject time gr ugr sdugr IL
40002.3 40002 3 0.4475618 0.3706000 0.02994533 0
40002.4 40002 4 0.4361786 0.3901111 0.01846110 0
40002.5 40002 5 0.4279880 0.4550000 0.02811839 0
40002.6 40002 6 0.4313647 0.4134444 0.04352974 0
40002.7 40002 7 0.4420889 0.4394286 0.02883143 0
40002.8 40002 8 0.4325227 0.3960000 0.06559222 0
I’m trying to do this with a for loop, but I’m a beginner in R and I’m having difficulties with this. The scoring formula I’m trying to implement is one where:
If the value in column 3 ($gr) is less that the difference between the value in column 4 ($ugr) and .35 times the value in column 5 ($sdugr), then the subject receives a 1, otherwise a 0.
What I’ve tried so far is:
for (i in kfdblock3to9$subject) {
if (kfdblock3to9$gr<(kfdblock3to9$ugr-(.35*kfdblock3to9$sdugr)))
kfdblock3to9$IL=1
else kfdblock3to9$IL=0
}
This gives me 50 warnings, all saying:
“the condition has length > 1 and only the first element will be used”
I suppose I’m doing something wrong with the indexes then, but I haven’t been able to figure it out. Any help is much appreciated.
Take a look at
withinandifelse:within()isn’t really that necessary, but it keeps your code a whole lot more readible and easier to understand.Why does it go wrong? That’s because your condition is vectorized : try
and you will see it returns a logical vector. Now an
if()clause can only deal with one boolean value at a time. If you have a vectorized result, you need a vectorized solution and that isifelse()