Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

One of the factor's levels is an empty string; how to replace it with non-missing value?

$
0
0

Data frame AEbySOC contains two columns - factor SOC with character levels and integer count Count:

> str(AEbySOC)
'data.frame':   19 obs. of  2 variables:
 $ SOC  : Factor w/ 19 levels "","Blood and lymphatic system disorders",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Count: int  25 50 7 3 1 49 49 2 1 9 ...

One of the levels of SOC is an empty character string:

> l = levels(AEbySOC$SOC)
> l[1]
[1] ""

I want to replace the value of this level by a non-empty string, say, "Not specified". This does not work:

> library(plyr)
> revalue(AEbySOC$SOC, c(""="Not specified"))
Error: attempt to use zero-length variable name

Neither does this:

> AEbySOC$SOC[AEbySOC$SOC==""] = "Not specified"
Warning message:
In `[<-.factor`(`*tmp*`, AEbySOC$SOC == "", value = c(NA, 2L, 3L,  :
  invalid factor level, NA generated

What's the right way to implement this? I appreciate any input/comment.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>