I am trying to change the name of an attribute in my spatial dataset. It is supposed to be an easy alteration, but for some reason it gives me an error message stating 'invalid factor level'. When I select the attribute it returns the correct name besides the message 415 levels. It confuses me what this means but I assume I can change this attribute name in an easy way. Help is much appreciated!

mun_neth$GM_NAAM[406]

[1] Súdwest-Fryslân 415 Levels: 's-Gravenhage 's-Hertogenbosch Aa en Hunze Aalburg Aalsmeer Aalten ... Zwolle

mun_neth$GM_NAAM[406] <- 'test'

Warning message: In [<-.factor(*tmp*, 406, value = c(20L, 28L, 32L, 332L, 80L, : invalid factor level, NA generated

有帮助吗?

解决方案

You can use

mun_neth$GM_NAAM <- as.factor(replace(as.character(mun_neth$GM_NAAM), 406, "test"))

This will convert the values to character strings before replacing. Afterwards, a factor is created.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top