How to change multiple columns' data type in R?
<p>I'd like to convert all fields ending with _FL from character to numeric. I thought this code will work, but is does not: all these fields are filled up with NAs. What's wrong with it?</p>

<pre><code>library(data.table)
#s = fread('filename.csv',header = TRUE,sep = ";",dec = ".")
s=data.table(ID=(1:10), B=rnorm(10), C_FL=c("I","N"), D_FL=(0:1), E_FL=c("N","I"))
cn=colnames(s)
# Change all fields ending with _FL from "N"/"I" to numeric 0/1
for (i in cn){
  if(substr(i,nchar(i)-2,nchar(i))=='_FL'){
    s[,i] = as.numeric(gsub("I",1,gsub("N",0,s[,i])))
  }
}
</code></pre>