0

I would like to summerize my data frame ( actually the frequence of each element in dataframe)

I use tablefunction to do this job:

a<-table(EndResult$Masstab)
a

but it returns me some elements with Null frequent!

                                                 .X20_TemperaturCPU 
                                                                0 
                              .Akt_Fuellprogramm.0..Rezept_Nummer 
                                                              534 
                   .Anwahl_Programm_Sorte.Programmnummer_Angef.0. 
                                                              510 
                     .Anwahl_Programm_Sorte.Sortennummer_Angef.0. 
                                                              180

if there is no such element, then actually it should not be showed this element.

if I am looking for such an element:

EndResult[(EndResult$Masstab==".X20_TemperaturCPU"),]

it returns me:

[1] Masstab Mf1MF2  MF1OF   MF2OF   MF1     MF2     OF     
<0 Zeilen> (oder row.names mit Länge 0)

why gives me this table function an element with the frequence of null?

Kaja
  • 2,962
  • 18
  • 63
  • 99

1 Answers1

1

R is providing you an output based on all the factor levels that it finds in the variable for which you are requesting frequencies. For example, consider the following.

> table(c("A","A","C","D","E","D","D","E","C"))

A C D E 
2 2 3 2 

> fac_version <- factor(x = c(1,1,3,4,5,4,4,5,3),
+                       levels = 1:5,
+                       labels = LETTERS[1:5])
> table(fac_version)
fac_version
A B C D E 
2 0 2 3 2

As you can see, by declaring a factor, the table command returns all the possible factor levels, including those which have zero values.

You probably want to set the option stringsAsFactors to be FALSE at the start of your program. Otherwise, R will coerce strings into factors, which I could imagine causing this problem later (if you, for instance, had rows with a set of strings that are filtered out before you run your frequency tables, you'll still get the returned rows with zero counts).

An alternative would be to create the table and filter out the zero values. Like this:

> table(fac_version)[table(fac_version) > 0]
fac_version
A C D E 
2 2 3 2
TARehman
  • 6,659
  • 3
  • 33
  • 60