Selecting by observation in R table

Posted by Stedy on Stack Overflow See other posts from Stack Overflow or by Stedy
Published on 2010-01-13T21:55:53Z Indexed on 2010/04/19 18:33 UTC
Read the original article Hit count: 617

Filed under:
|

I was working through the Rosetta Code example of the knapsack problem in R and I came up with four solutions. What is the best way to output only one of the solutions based on the observation number give in the table?

    > values[values$value==max(values$value),]
     I II III value weight volume
2067 9  0  11 54500     25     25
2119 6  5  11 54500     25     25
2171 3 10  11 54500     25     25
2223 0 15  11 54500     25     25

My initial idea was to save this output to a new variable, then query the new variable such as:

> newvariable <- values[values$value==max(values$value),]
> newvariable2 <- newvariable[2,]
> newvariable2
     I II III value weight volume
2119 6  5  11 54500     25     25

This gives me the desired output but is there a cleaner way to do it?

© Stack Overflow or respective owner

Related posts about r

    Related posts about rosetta-stone