r - One of the factor's levels is an empty string; how to replace it with non-missing value? -
data frame aebysoc contains 2 columns - factor soc character levels , integer count count:
> str(aebysoc) 'data.frame': 19 obs. of 2 variables: $ soc : factor w/ 19 levels "","blood , 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 levels of soc empty character string:
> l = levels(aebysoc$soc) > l[1] [1] ""
i want replace value of level non-empty string, say, "not specified". not work:
> library(plyr) > revalue(aebysoc$soc, c(""="not specified")) error: attempt use zero-length variable name
neither 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 right way implement this? appreciate input/comment.
levels(aebysoc$soc)[1] <- "not specified"
created toy example:
df<- data.frame(a= c("", "a", "b")) df # #1 #2 #3 b levels(df$a) #[1] "" "a" "b" levels(df$a)[1] <- "not specified" levels(df$a) #[1] "not specified" "a" "b"
edit
as per op's comments if need find according value in such case, can try
levels(aebysoc$soc)[levels(aebysoc$soc) == ""] <- "not specified"
Comments
Post a Comment