user3969377
user3969377

Reputation:

R How to print non zero frequency table values in rows

Table prints rows and columns and the number of occurrences at the intersections. I have a large table that is almost all zeros. Can table be printed in rows instead, with the first column being the name of the table row, the second column being the name of the table column, and the third column being the number of occurrences.

library(data.table)
nSamples=10
set.seed(13)
d <- data.table(id=sample(1001:1010,nSamples,replace=T),
                own=sample(paste('h-',7:10,sep=''),nSamples,replace=T))
tbl<-table(d)
print(tbl)

The output looks like

      own
id     h-10 h-8 h-9
  1001    0   1   2
  1003    1   0   0
  1004    1   0   0
  1006    0   1   0
  1008    0   0   2
  1009    1   0   0
  1010    0   0   1

The output I would like is

     id  own Freq
   1003 h-10    1
   1004 h-10    1
   1009 h-10    1
   and so on

Note: the question was modified by correcting the set.seed.

The answer provided below gives the desired result

R> subset(as.data.frame(table(d)),Freq>0)
     id  own Freq
2  1003 h-10    1
3  1004 h-10    1
6  1009 h-10    1
8  1001  h-8    1
11 1006  h-8    1
15 1001  h-9    2
19 1008  h-9    2
21 1010  h-9    1

Upvotes: 1

Views: 2338

Answers (1)

akrun
akrun

Reputation: 887691

1) To convert to long format

as.data.frame(table(d))

or if you need a data.table

as.data.table(table(d))

2) set.seed issue

You are assigning <- set.seed to 1. The correct way would be:

set.seed(1)

3) For subsetting Freq >0

setkey(as.data.table(table(d)),N)[N>0]

If there are only 0s and 1s

setkey(as.data.table(table(d)),N)[J(1)]

or the data.frame approach

subset(as.data.frame(table(d)),Freq>0)

EDIT

You can avoid using the table and then converting back to long form (as commented by @Arun). Instead

 d[, .N, by=names(d)]

Upvotes: 3

Related Questions