0

I have a data set with 95188 rows and 3 columns. I want to count the number of the maximum repeating elements in a column. What shall I code in R for this?

I have tried following

length(which(net2=="1")) 
[1] 69400
> length(which(net2=="2")) 
[1] 14250
> length(which(net2=="37")) 
[1] 36

This counts the individual in all figures available.

On request please consider following data set:

26391  3038  3039     1
26392  3038  3040     1
26393  3038  3041     1
26394  3038  3042     2
26395  3038  3598     1
26396  3038  5632     1
26397  3038  5633     1
26398  3038  5634     1
26399  3038  5635     1
26400  3038  5636     1
26401  3038  5637     1
26402  3039   800     1
26403  3039  2068     2
26404  3039  2069     1
26405  3039  3036     1
26406  3039  3037     2
26407  3039  3038     1
26408  3039  3040     1
26409  3039  3041     1
26410  3039  3042     1
26411  3039  5961     1
26412  3039  5962     1
26413  3039  5963     1
26414  3039 16449     1
26415  3039 16450     1
26416  3039 16451     1
26417  3039 16452     1
26418  3040   800     1
26419  3040  2068     1
26420  3040  2069     1
26421  3040  3036     1
26422  3040  3037     1
26423  3040  3038     1
26424  3040  3039     1
26425  3040  3041     1
26426  3040  3042     1
26427  3041   800     1
26428  3041  2068     1
26429  3041  2069     1
26430  3041  3036     1
26431  3041  3037     1
26432  3041  3038     1
26433  3041  3039     1
26434  3041  3040     1
26435  3041  3042     2
26436  3041  4023     1
26437  3041  5726     1
26438  3041  6190     1
26439  3041  6191     1
26440  3041  7025     1
26441  3041  7371     1
26442  3041  7372     1
26443  3041  9743     1
26444  3041  9744     1
26445  3041 11313     1
26446  3041 11314     1
26447  3041 11315     1
26448  3041 11316     1
26449  3042   799     3
26450  3042   800     1
26451  3042  2068     6
26452  3042  2069     1
26453  3042  2469     1
26454  3042  2470     1
26455  3042  2505     2
26456  3042  2645     1
26457  3042  2650     1
26458  3042  3036     2
26459  3042  3037     1
26460  3042  3038     2
26461  3042  3039     1
26462  3042  3040     1
26463  3042  3041     2
26464  3042  3683     1
26465  3042  4023     1
26466  3042  4049     2
26467  3042  4295     2
26468  3042  4723     1
26469  3042  4756     1
26470  3042  5152     1
26471  3042  5153     3
26472  3042  5154     4
26473  3042  5255     4
26474  3042  5256     3
26475  3042  5632     2
26476  3042  5633     1
26477  3042  5634     1
26478  3042  5635     2
26479  3042  5636     2
26480  3042  5637     1
26481  3042  5726     1
26482  3042  6190     1
26483  3042  6191     1
26484  3042  6602     1
26485  3042  7025     1
26486  3042  7284     1
26487  3042  7371     1
26488  3042  7372     1
26489  3042  7862     1
26490  3042  9060     1
26491  3042  9242     1
26492  3042  9356     2
26493  3042  9357     3
26494  3042  9358     3
26495  3042  9359     1
26496  3042  9360     1
26497  3042  9723     1
26498  3042  9743     1
26499  3042  9744     1
26500  3042 10591     3
26501  3042 10592     3
26502  3042 10915     1
26503  3042 11176     1
26504  3042 11313     1
26505  3042 11314     1
26506  3042 11315     1
26507  3042 11316     1
26508  3042 11395     6
26509  3042 11836     1
26510  3042 12234     6
26511  3042 12235     5
26512  3042 12757     3
26513  3042 12758     2
26514  3042 13299     1
26515  3042 13300     1
26516  3042 13301     1
26517  3042 13688     1
26518  3042 14147     1
26519  3042 14148     1
26520  3042 14149     1
26521  3042 14303     1
26522  3042 15501     1
26523  3042 15513     1
26524  3042 15924     1
26525  3043  3044     3
26526  3043  3687     1
26527  3043  3688     1
26528  3043  3689     1
26529  3043  4167     1
26530  3044  3043     3
26531  3044  4167     1
26532  3045   193     6
26533  3045  1541     2
26534  3045  2517     2
26535  3045  3046     1
26536  3045  3126     3
26537  3045  3127     3
26538  3045  3944     1
26539  3045  4642     1
26540  3045  4713     2
26541  3045  4977     1
26542  3045  6518     1
26543  3045  6520     1
26544  3045 11980     1
26545  3045 12244     3
26546  3045 15303     1
26547  3046  3045     1
26548  3046 12387     1
26549  3046 12388     1
26550  3047   454     4
26551  3048     4     6
26552  3048     6     3
26553  3048     7     1
26554  3048  2212     1
26555  3048  3232     2
26556  3048  8785     1

The first column is just serial number. Column 2 and column 3 shows the combination. I need to find the element repeating maximum in column 1 and column 2.

Henrik
  • 65,555
  • 14
  • 143
  • 159
cts
  • 1
  • 4
  • 1
    @Kay Nelson: please use code markdown only for code. – S.L. Barth is on codidact.com Oct 28 '13 at 10:41
  • 1
    Hi and welcome to stackoverflow! You are much more likely to receive a helpful answer if you provide a [minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Please also clarify your desired results. Thanks! – Henrik Oct 28 '13 at 10:42
  • take a look at `?table`. `table(net2)` will tell you the frequency of each element in `net` or `sapply(net, table)` if `net` is a data.frame . – Jilber Urbina Oct 28 '13 at 10:43
  • Hi I have 3 columns and so many different elements. But how do I find which one have maximum repeats? – cts Oct 28 '13 at 10:55
  • 1
    Perhaps have a look at the rle function. Type ?rle into your R Console and hit enter. Also, from looking at your other questions on SO, it is clear that you should look over other people's questions on SO to see how they provide sample data, desired output, etc. as Henrik suggested. You need to make more effort to help others help you. Additionally, there are several other questions on SO that deal with run length encoding, which is what I think you are asking about, but I am not positive. Cheers. – Jota Dec 02 '13 at 19:49

3 Answers3

2
set.seed(42)
DF <- data.frame(a=sample(c("a","b","c"),20,TRUE), b=sample(1:3,20,TRUE))
counts <- table(DF$a)
counts[which.max(counts)]
#c 
#9
Roland
  • 127,288
  • 10
  • 191
  • 288
0

You can use table() as stated in the comments:

a <- table(net2)

then you can get the number of occurences by providing the number as the column name:

a[names(a)==1]

or if you want yoy can convert to dataframe:

as.data.frame(table(net2))

or you can just use

sum(net2 == 1)

which will sum all the TRUE values where net2 == 1, so basically it will give you a count of elements

yemu
  • 26,249
  • 10
  • 32
  • 29
0
a<-matrix( c( 1:2 , 2:1 , 4 , 1 , 1 , 2 ) ,nrow=4 )

a

> a

     [,1] [,2]
[1,]    1    4
[2,]    2    1
[3,]    2    1
[4,]    1    2

length(which(a[,1]=="4"))  # shows frequency of 4 in 1st col

length(which(a[,1]=="3"))  # shows frequency of 4 in 1st col

This also works:

table(a[,1])
table(a[,2])
  • Hi Akash, I want to know which element is repeating maximum from the given above set. – cts Oct 28 '13 at 11:18