I have a dataset and one of its columns has factor levels "a" "b" "c" "NotPerformed" . How to change all "NotPerformed" factors to NA?
"a" "b" "c" "NotPerformed"
"NotPerformed"
Set the NA level:
x <- factor(c("a", "b", "c", "NotPerformed")) x ## [1] abc NotPerformed ## Levels: abc NotPerformed levels(x)[levels(x)=='NotPerformed'] <- NA x ## [1] abc <NA> ## Levels: abc
Note that the factor level is removed.
I am reviewing my old answer and providing what you can do from September 2016. With the development of the dplyr package, you can now use recode_factor() to do the job.
dplyr
recode_factor()
x <- factor(c("a", "b", "c", "NotPerformed")) # [1] abc NotPerformed # Levels: abc NotPerformed library(dplyr) recode_factor(x, NotPerformed = NA_character_) # [1] abc <NA> # Levels: abc
Source: https://habr.com/ru/post/973944/More articles:Remove URL from string in R - stringRemove shadow between Actionbar and Tab - androidWhy does document.getElementById return an object named "value"? - javascripthikaricp implementation with Microsoft SQL Server - javaHow to get user zipcode using AngularJS - javascriptIs it possible to use Intvar / DoubleVar in a Python thread? - pythonPython / Tkinter: is Tkinter StringVar thread safe (IntVar etc.)? - pythonWhy can't I add a string to NSURL? - swiftThe boot table does not work correctly in Chrome, but works fine in Firefox - javascriptWkhtmltopdf error while generating pdf with a large number of pages with header / footer - ruby | fooobar.comAll Articles