[R] use table function with data frame subsets

message letter at openmailbox.org
Mon Feb 20 22:10:20 CET 2017


Readers,

Data set:

20170101,10020,A,b,Y
20170101,10020,B,b,N
20170101,10020,C,d,Y
20170102,20001,C,d,Y
20170102,20001,D,m,Y
20170102,20001,L,a,Y

testtable<-read.csv('~/tmp/data.csv',header=F)
testtablea<-testtable[grep('^10',testtable[,2]),]
> testtable
         V1    V2 V3 V4 V5
1 20170101 10020  A  b  Y
2 20170101 10020  B  b  N
3 20170101 10020  C  d  Y
4 20170102 20001  C  d  Y
5 20170102 20001  D  m  Y
6 20170102 20001  L  a  Y
> testtablea
         V1    V2 V3 V4 V5
1 20170101 10020  A  b  Y
2 20170101 10020  B  b  N
3 20170101 10020  C  d  Y


> table(testtable[,4],testtable[,5])

     N Y
   a 0 1
   b 1 1
   d 0 2
   m 0 1
> table(testtablea[,4],testtablea[,5])

     N Y
   a 0 0
   b 1 1
   d 0 1
   m 0 0

Wy do values for rows beginning 'a' and 'm' appear when they do not 
satisfy the regular expression for the object 'testtablea'?

Please, how to use the 'table' function to show:

> table(testtablea[,4],testtablea[,5])

     N Y
   b 1 1
   d 0 1

Thanks.



More information about the R-help mailing list