I am getting a warning: "the condition has length > 1 and only the first element will be used", when I try to use the following function fixED
:
> fixED
function(x) {
if(nchar(x) == 5) {
return(x)
}
else if(nchar(x) == 3) {
return(gsub('^(.{2})(.*)$', '\\100\\2', x))
}
else if(nchar(x) == 4) {
return(gsub('^(.{2})(.*)$', '\\10\\2', x))
}
}
Here is the full set of warnings:
Warning messages:
1: In if (nchar(x) == 5) { :
the condition has length > 1 and only the first element will be used
2: In if (nchar(x) == 4) { :
the condition has length > 1 and only the first element will be used
3: In if (nchar(x) == 3) { :
the condition has length > 1 and only the first element will be used
I know that there are a few other questions on here that are similar to this but none of them has clarified for me what the problem may be. It sounds to me as though R things that the logical expressions inside the if statements are returning a vector of >1.
However, I don't understand how that could be the case since I'm fairly certain that they are a logical vector of 1. Indeed, the following seems to demonstrate that:
> length((nchar("43") ==2))
[1] 1
Can anyone see what the problem is here? I'm stumped at the moment. The data to which I'd like to apply this function looks like this:
> df.short
AD ED
9918 57 84
9919 57 84
9920 57 84
9921 57 84
9922 57 84
9923 57 84
9924 57 84
9925 57 85
9926 57 85
9927 57 85
9928 57 85
I apply it like so:
df.short$test <- fixED(paste(df.short$AD, df.short$ED, sep=""))