Re: [R] how to rearrange a dataframe

2010-02-23 Thread David Winsemius


On Feb 23, 2010, at 4:27 PM, Laura Rodriguez Murillo wrote:


Hi all,

I'd appreciate if anyone can help me with this...

I have a data frame that looks like this:

1 + name1 1 2 3
2 + name2 5 9 10
2 - name3 56 74 93
1 - name4 65 75 98

I need to rearrange this in a way so that the rows with  1 in the
first column, and - in the second column; then columns 4 and 6
should switch places. That is, column 6 would be now column 4 and
column 4 would be column 6 (column 5 should stay as column 5)
In the same way, if the first column is 2 and the second is +,
then the same rearrangement should be done.
Rows with the first two entries 1 + or 2 - should stay in the same  
order.

This should be done for each row independently.


Seems as though there should be a swap function or a fancy indexing  
solution that would do this but I am not coming up with anything  
particularly terse. Here is a beginning. You could assign these  
vectors to new columns (perhaps after (re-)making them numeric ) and  
delete the old columns if you needed to. Say your dataframe were  
called ta:


 apply(ta, 1, function(x) ifelse( (x[1]==1  x[2]==-)|(x[1]==2   
x[2]==+), c(x[6],x[4]), c(x[4],x[6]) ))

[1]  1 10 56 98
 apply(ta, 1, function(x) ifelse( (x[1]==1  x[2]==-)|(x[1]==2   
x[2]==+), c(x[4],x[6]) , c(x[6],x[4])))

[1]  3  5 93 65

--
David.


Thanks a lot for your help!

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


__
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] how to rearrange a dataframe

2010-02-23 Thread Tom Short
Try this:

a - b - read.table(textConnection(
1 + name1 1 2 3
2 + name2 5 9 10
2 - name3 56 74 93
1 - name4 65 75 98), skip=1, header=FALSE)

swapidx - with(a, (V1 == 2  V2 == +) | (V1 == 1  V2 == -))
b[swapidx,] - b[swapidx, c(1:3,6:4)]

This creates an indexing vector that identifies which rows to swap,
then the 6:4 flips around the fourth through sixth columns.

- Tom

On Tue, Feb 23, 2010 at 5:27 PM, Laura Rodriguez Murillo
laura.lmuri...@gmail.com wrote:
 Hi all,

 I'd appreciate if anyone can help me with this...

 I have a data frame that looks like this:

 1 + name1 1 2 3
 2 + name2 5 9 10
 2 - name3 56 74 93
 1 - name4 65 75 98

 I need to rearrange this in a way so that the rows with  1 in the
 first column, and - in the second column; then columns 4 and 6
 should switch places. That is, column 6 would be now column 4 and
 column 4 would be column 6 (column 5 should stay as column 5)
 In the same way, if the first column is 2 and the second is +,
 then the same rearrangement should be done.
 Rows with the first two entries 1 + or 2 - should stay in the same order.
 This should be done for each row independently.

 Thanks a lot for your help!

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


__
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] how to rearrange a dataframe

2010-02-23 Thread Peter Alspach
Tena koe Laura

temp - yourData[(yourData[,1]==1  yourData[,2]=='-') |
(yourData[,1]==2  yourData[,2]=='+'),6]

Then use similar subsetting to put column 4 into 6, and then temp into
column 4.  If you don't want to use the intermediary temp then you can
use the construct

a - a+b
b - a-b
a - a-b

HTH 

Peter Alspach

 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of Laura 
 Rodriguez Murillo
 Sent: Wednesday, 24 February 2010 11:28 a.m.
 To: r-help@r-project.org
 Subject: [R] how to rearrange a dataframe
 
 Hi all,
 
 I'd appreciate if anyone can help me with this...
 
 I have a data frame that looks like this:
 
 1 + name1 1 2 3
 2 + name2 5 9 10
 2 - name3 56 74 93
 1 - name4 65 75 98
 
 I need to rearrange this in a way so that the rows with  1 
 in the first column, and - in the second column; then 
 columns 4 and 6 should switch places. That is, column 6 would 
 be now column 4 and column 4 would be column 6 (column 5 
 should stay as column 5) In the same way, if the first column 
 is 2 and the second is +, then the same rearrangement 
 should be done.
 Rows with the first two entries 1 + or 2 - should stay in the 
 same order.
 This should be done for each row independently.
 
 Thanks a lot for your help!
 
 __
 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.
 

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