0

With mytable <- table(VarA, varB) I've created a frequency table that looks like this:

     1  2
0    0  5
1    5  7
2    0  0
3    7  9
4    0  0
5    5 10

Now I want to run some statistics with assocstats and table2d_summary on that table. But first I want to delete the empty values. That means the rows in the table with zero counts,

So how can I get rid of the rows with both frequencies of 0 (in my example it's row 2 and 4)?

For a data frame I could just use the subsetfunction:

mytable_noZ <- subset(mytbale, mytable$VarA != 0 & mytable$VarB != 0)

But it doesn't seem to be possible to refer to a variable (a column) by this method in a table.

A way that worked was deleting the rows "manually"

mytable_noZ <- mytable[-c(2,4),]

Which is easy in this example but I've some bigger tables.

I searched for a solution here and elsewhere but always only found ways to delete rows, variables and cases in data frames.

So hopefully someone can help me on this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mobody
  • 46
  • 6
  • 1
    You can convert it to a data.frame with `mytablenew <- as.data.frame.matrix(mytable)` and then do the `subset` as you showed – akrun Jan 15 '18 at 10:27
  • Thanks! That was helpfull. – Mobody Jan 15 '18 at 11:16
  • I had tried this way but only with `as.data.frame(mytable)`. Which gave me a data frame with case counts. Now I'm stuck in converting my data frame back into a table (which is necessary for the `assocstats`function). I tried to convert with `as.table(mytbalenew)`. But it just says it can't convert. – Mobody Jan 15 '18 at 11:23
  • another possibility would be to drop the levels from you VarA, that don't appear in your data. Then you don't get the all-zero-lines from the beginning. – scheddy Jan 15 '18 at 11:27
  • That would also be an idea but seems to me the more complicated way. Since I first need to find out which are the levels of VarA which don't appear. – Mobody Jan 16 '18 at 08:09

1 Answers1

1

I finally found a solution to my question and want to present it here. Just in case someone else comes across this problem.

First I transformed the table into a data.frame (like akrun suggested):

mydf <- as.data.frame.matrix(mytable)

Then I chose the rows to drop and saved it into the variable deleterows and used this variable to delete the rows from the original table:

deleterows <- which(mydf$`1` == 0 & mydf$`2` == 0)
mytable <- mytable[-deleterows,]
Mobody
  • 46
  • 6