Try:
pvalsig1$Zeitpunkt <- factor(gsub("\\s*", "", pvalsig1$Zeitpunkt), levels=c("ZP0", "ZP2", "ZP4", "ZP7", "ZP14", "ZP21", "ZP28", "ZP35", "ZPM9", "ZPM9+1"))
This will remove all spaces from your column. The problem you're having is you're trying to create a factor on with values like "ZP0 "
with levels ZP0
, so the values aren't matching due to the extra spaces.
Note this will break if your factor levels can contain spaces or other blank characters, but if that's the case you can adjust the regular expression pretty easily to something like:
"(^\\s+|\\s*$)"
Also, depending on where you're getting this data from, some of the input functions have facilities to strip extra white space (e.g. read.table
has a strip.white
argument).
Also, quick search on R trim pulls up this popular SO answer.