[R] Trouble with arguments to 'order'

2013-05-28 Thread Barry King
I have an Excel worksheet with 20 rows.  Using XLConnect I successfully
read the data into 'indata'.  In order to sort it on the 'Item' column
and the 'Price_Per_Item' column I submit:

index - with(indata, order(Item, Price_Per_Item))
sortedData - indata[index, ]

The above works fine but now I do not want to name the columns in the
R program directly but pass the names of the columns from a parameter file:

index - with(indata, order(parameters$ItemColumn,
parameters$PriceColumn))
sortedData - indata[index, ]

This does not work. Only one row appears in 'sortedData'.  I've tried
unlisting the two arguments to 'order' but this does not correct the
problem.

Can anyone suggest a solution to my problem?  Your assistance is
appreciated.

- Barry King

-- 
__
*Barry E. King, Ph.D.*
Director of Retail Operations
Qualex Consulting Services, Inc.
barry.k...@qlx.com
O: (317)940-5464
M: (317)507-0661
__

[[alternative HTML version deleted]]

__
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.


Re: [R] Trouble with arguments to 'order'

2013-05-28 Thread peter dalgaard

On May 28, 2013, at 08:06 , Barry King wrote:

 I have an Excel worksheet with 20 rows.  Using XLConnect I successfully
 read the data into 'indata'.  In order to sort it on the 'Item' column
 and the 'Price_Per_Item' column I submit:
 
 index - with(indata, order(Item, Price_Per_Item))
 sortedData - indata[index, ]
 
 The above works fine but now I do not want to name the columns in the
 R program directly but pass the names of the columns from a parameter file:
 
 index - with(indata, order(parameters$ItemColumn,
parameters$PriceColumn))
 sortedData - indata[index, ]
 
 This does not work. Only one row appears in 'sortedData'.  I've tried
 unlisting the two arguments to 'order' but this does not correct the
 problem.
 
 Can anyone suggest a solution to my problem?  Your assistance is
 appreciated.

Nothing to do with order as such, but parameters$ItemColumn is presumably a 
character value (vector of length 1). So you seem to be effectively doing 
order(A,B) which is the constant 1. You need to spell out your intentions:

order(indata[[parameters$ItemColumn]], indata[[parameters$PriceColumn]])

or maybe

do.call(order, indata[unlist(parameters[c(ItemColumn,PriceColumn)])])

(both untested)


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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.


Re: [R] Trouble with arguments to 'order'

2013-05-28 Thread Patrick Burns

If I understand your problem correctly,
you want to use '[[' instead of '$':

order(parameters[[ItemColumn]], parameters[[PriceColumn]])



On 28/05/2013 07:06, Barry King wrote:

I have an Excel worksheet with 20 rows.  Using XLConnect I successfully
read the data into 'indata'.  In order to sort it on the 'Item' column
and the 'Price_Per_Item' column I submit:

index - with(indata, order(Item, Price_Per_Item))
sortedData - indata[index, ]

The above works fine but now I do not want to name the columns in the
R program directly but pass the names of the columns from a parameter file:

index - with(indata, order(parameters$ItemColumn,
 parameters$PriceColumn))
sortedData - indata[index, ]

This does not work. Only one row appears in 'sortedData'.  I've tried
unlisting the two arguments to 'order' but this does not correct the
problem.

Can anyone suggest a solution to my problem?  Your assistance is
appreciated.

- Barry King



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')

__
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.


Re: [R] Trouble with arguments to 'order'

2013-05-28 Thread Barry King
Thank you for your assistance.  I went with Peter's solution.  All is
working well now.
- Barry


On Tue, May 28, 2013 at 4:12 AM, Patrick Burns pbu...@pburns.seanet.comwrote:

 If I understand your problem correctly,
 you want to use '[[' instead of '$':

 order(parameters[[ItemColumn**]], parameters[[PriceColumn]])




 On 28/05/2013 07:06, Barry King wrote:

 I have an Excel worksheet with 20 rows.  Using XLConnect I successfully
 read the data into 'indata'.  In order to sort it on the 'Item' column
 and the 'Price_Per_Item' column I submit:

 index - with(indata, order(Item, Price_Per_Item))
 sortedData - indata[index, ]

 The above works fine but now I do not want to name the columns in the
 R program directly but pass the names of the columns from a parameter
 file:

 index - with(indata, order(parameters$ItemColumn,
  parameters$PriceColumn))
 sortedData - indata[index, ]

 This does not work. Only one row appears in 'sortedData'.  I've tried
 unlisting the two arguments to 'order' but this does not correct the
 problem.

 Can anyone suggest a solution to my problem?  Your assistance is
 appreciated.

 - Barry King


 --
 Patrick Burns
 pbu...@pburns.seanet.com
 twitter: @burnsstat @portfolioprobe
 http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog
 http://www.burns-stat.com
 (home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')




-- 
__
*Barry E. King, Ph.D.*
Director of Retail Operations
Qualex Consulting Services, Inc.
barry.k...@qlx.com
O: (317)940-5464
M: (317)507-0661
__

[[alternative HTML version deleted]]

__
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.