R code generator uses factor levels for numeric subsetting
Created by: mwip
#From the top of the script, reading data with read.dta()
will create factor levels for variables containing letters.
pfad <- read.dta(file.path(path_in, "ppfad.dta"))
When subsetting the data based on such variables (e.g. $netto), R cannot use the numerical information within the [xx]
directly.
pfad <- with(pfad, pfad[ (xnetto >= 10 & xnetto < 20) | (wnetto >= 10 & wnetto < 20) | (ynetto >= 10 & ynetto < 20) , ])
One possible solution might be to use the first characters of the factor levels to generate signed integers which could then be subsetted.
for (i in grep("netto", colnames(pfad))){
pfad[,i] <- as.integer(gsub("\\[|\\]", "", substr(pfad[,i], 1, 4)))
}
However, it should be considered to copy the data bevorehand, in order to keep the information.