[R] compare and count data

David L Carlson dcarlson at tamu.edu
Mon Jan 21 17:29:42 CET 2013


Another approach

> critical<- c(1.61,75.89, 1.61, 75.89)
> colSums(sweep(mydata, 2, critical, "<"))
V1 V2 V3 V4 
 0 19  0  0

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jim Lemon
> Sent: Monday, January 21, 2013 1:17 AM
> To: Roslina Zakaria
> Cc: r-help at r-project.org
> Subject: Re: [R] compare and count data
> 
> On 01/21/2013 04:39 PM, Roslina Zakaria wrote:
> > Dear r-users,
> >
> > I have these data below:
> >
> > I would like to compare each column with a certain value and count
> how many in each column less than that specified value.
> >
> > odd column (1,3,5) will compare with 1.61 and even column (2,4,6)
> will compare with 75 and I would like to count how many for each
> column.
> >
> >
> > I tried these below but it give me just one set of data.
> >
> > critical<- c(1.61,75.89, 1.61, 75.89, 1.61, 75.89)   ; critical
> > pilih<- mydata[mydata<  critical] ; pilih
> > jumlah<- length(pilih)   ; jumlah
> > kuasa<- 1 - (jumlah/1000);  kuasa
> >
> >> head(mydata,20)
> >           V1       V1       V1       V1
> > 1  3.014505 62.96425 3.014505 138.0673
> > 2  2.817503 56.03400 2.817503 133.3411
> > 3  2.976227 47.12192 2.976227 139.2438
> > 4  2.825495 75.05284 2.825495 129.2959
> > 5  2.793500 52.75190 2.793500 130.9874
> > 6  3.006333 54.56210 3.006333 136.2982
> > 7  3.245833 71.31358 3.245833 150.0974
> > 8  2.559980 64.28300 2.559980 114.5395
> > 9  2.548456 69.48727 2.548456 118.7383
> > 10 2.715040 58.64134 2.715040 123.0574
> > 11 2.799652 58.96281 2.799652 126.9216
> > 12 2.900839 75.43427 2.900839 131.6100
> > 13 3.183844 63.57657 3.183844 143.5530
> > 14 2.792940 63.04534 2.792940 120.1010
> > 15 3.229830 71.09790 3.229830 136.6931
> > 16 3.528115 68.64850 3.528115 156.0135
> > 17 2.973711 58.45141 2.973711 128.6738
> > 18 2.518891 53.62788 2.518891 110.6072
> > 19 2.848493 79.29142 2.848493 127.6110
> > 20 2.971362 57.81008 2.971362 129.5411
> > 	[[alternative HTML version deleted]]
> >
> >
> Hi Roslina,
> I think what you may want is something like this:
> 
> colSums(t(apply(mydata,1,FUN="<",rep(c(1.61,75),
>   length.out=dim(mydata)[2]/2))))
> 
> Jim
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list