2

How can I identify and remove entries in a list that are empty, but aren't NA or NULL. Why does my list appear to be a factor when I try to compare its elements?

> s <- subset(world, (oil == "Yes") & !is.na(effectiveness) & !is.null(effectiveness), select = effectiveness)
> s
        effectiveness
3   32.62411409517834
30  31.91489350429382
41                   
54   23.8770690651932
64  35.93380642839076
80   33.3333332769696
81   35.6973993299137
116   50.118203450602
131 20.09456253502668
181                  
194 19.62174974716583
> typeof(s)
[1] "list"
> sum(s)
Error in FUN(X[[1L]], ...) : 
  only defined on a data frame with all numeric variables
> s[s > 0]
 [1] NA NA NA NA NA NA NA NA NA NA NA
Warning message:
In Ops.factor(left, right) : > not meaningful for factors
Karsten W.
  • 17,826
  • 11
  • 69
  • 103
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243

1 Answers1

2

s is a data.frame. typeof returns the underlying storage type, not the class. It's hard to know why s$effectiveness is a factor without knowing how you created s.

You can convert a factor to numeric via:

s$effectiveness <- as.numeric(levels(s$effectiveness))[s$effectiveness]
Community
  • 1
  • 1
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thanks! That appears to work. Why can't I take the mean of the result? I get that the typeof the result is "double", yet when I try to apply `mean`, I get NA. – Rose Perrone Apr 23 '14 at 16:58
  • 1
    mean(n, na.rm=T), as the as.numeric will introduce NA's where you have blanks – infominer Apr 23 '14 at 16:59