Sorry for the newbie question, I am trying to loop through a dataframe and replace binary string values like "YES" and "NO" with 0 and 1.
I'm trying really hard to learn the syntax but it just defies logic. Rather than create a line for each factor like this 'dat2$Verified.as.Malware <- ifelse( is.na(dat2$Verified.as.Malware), 'No ', 'Yes' )' I thought I would write a function like this :
replaceStringBinary = function(dataFrame){
for( currentRow in 1:nrow(dataFrame)){
for( currentCol in 1:ncol(dataFrame)){
if ( is.na(dataFrame[currentRow, currentCol]) ){
dataFrame[currentRow, currentCol] = 0
}
if( dataFrame[currentRow, currentCol] == 'Yes'){
dataFrame[currentRow, currentCol] = 1
}
if ( dataFrame[currentRow, currentCol] == 'No'){
dataFrame[currentRow, currentCol] = 0
}
}
}
}
which I feel is syntactically correct but throws this error:
Error in if (dataFrame[currentRow, currentCol] == "No") { :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In [<-.factor(*tmp*, iseq, value = 0) :
invalid factor level, NA generated
2: In [<-.factor(*tmp*, iseq, value = 1) :
invalid factor level, NA generated
I account for the 'NA' test in the first if statement to ensure the subsequent if statements do not test a non string but I cant work out why it throws this error.
Can any one assist please?