try this: > x <- read.table(textConnection(" value0 value60 value120 value180 value240 > value300 + 1 -13007 -11707 -11072 -12471 -12838 -13357 + 2 -12838 -13210 -11176 -11799 -13210 -13845 + 3 -12880 -11778 -11113 -12439 -13089 -13880 + 4 -12805 -11653 -11071 -12385 -11561 -13317 + 5 -12834 -13527 -11067 -11638 -13527 -13873 + 6 -11068 -11698 -12430 -12430 -12430 -12814 + 7 -12807 -14068 -11092 -11709 -11607 -13025 + 8 -12770 -11665 -11061 -12373 -11426 -12805 + 9 -12988 -11736 -11137 -12570 -13467 -13739 + 10 -11779 -12873 -12973 -12537 -12973 -11146"), header=TRUE) > closeAllConnections() > # generate the indices of 1st&2nd largest in each row > indx <- apply(x, 1, function(z){ + order(z, decreasing=TRUE)[1:2] + }) > # now print out the data for each row > for (i in seq(ncol(indx))){ + cat('row:', i, + '1st:', x[i, indx[1,i]], 'col:', colnames(x)[indx[1,i]], + '2nd:', x[i, indx[2,i]], 'col:', colnames(x)[indx[2,i]], '\n') + } row: 1 1st: -11072 col: value120 2nd: -11707 col: value60 row: 2 1st: -11176 col: value120 2nd: -11799 col: value180 row: 3 1st: -11113 col: value120 2nd: -11778 col: value60 row: 4 1st: -11071 col: value120 2nd: -11561 col: value240 row: 5 1st: -11067 col: value120 2nd: -11638 col: value180 row: 6 1st: -11068 col: value0 2nd: -11698 col: value60 row: 7 1st: -11092 col: value120 2nd: -11607 col: value240 row: 8 1st: -11061 col: value120 2nd: -11426 col: value240 row: 9 1st: -11137 col: value120 2nd: -11736 col: value60 row: 10 1st: -11146 col: value300 2nd: -11779 col: value0 > >
On Fri, Jul 23, 2010 at 9:20 PM, <mpw...@illinois.edu> wrote: > I have a data frame with a couple million lines and want to retrieve the > largest and second largest values in each row, along with the label of the > column these values are in. For example > > row 1 > strongest=-11072 > secondstrongest=-11707 > strongestantenna=value120 > secondstrongantenna=value60 > > Below is the code I am using and a truncated data.frame. Retrieving the > largest value was easy, but I have been getting errors every way I have tried > to retrieve the second largest value. I have not even tried to retrieve the > labels for the value yet. > > Any help would be appreciated > Mike > > >> data<-data.frame(value0,value60,value120,value180,value240,value300) >> data > value0 value60 value120 value180 value240 value300 > 1 -13007 -11707 -11072 -12471 -12838 -13357 > 2 -12838 -13210 -11176 -11799 -13210 -13845 > 3 -12880 -11778 -11113 -12439 -13089 -13880 > 4 -12805 -11653 -11071 -12385 -11561 -13317 > 5 -12834 -13527 -11067 -11638 -13527 -13873 > 6 -11068 -11698 -12430 -12430 -12430 -12814 > 7 -12807 -14068 -11092 -11709 -11607 -13025 > 8 -12770 -11665 -11061 -12373 -11426 -12805 > 9 -12988 -11736 -11137 -12570 -13467 -13739 > 10 -11779 -12873 -12973 -12537 -12973 -11146 >> #largest value in the row >> strongest<-apply(data,1,max) >> >> >> #second largest value in the row >> n<-function(data)(1/(min(1/(data[1,]-max(data[1,]))))+ (max(data[1,]))) >> secondstrongest<-apply(data,1,n) > Error in data[1, ] : incorrect number of dimensions >> > > ______________________________________________ > R-help@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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ R-help@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.