Re: [R] Simple use of dcast (reshape2 package)

2013-01-23 Thread Patrick Connolly
On Tue, 22-Jan-2013 at 08:30AM -0500, Ista Zahn wrote:

| Hi,
| 
| ID is not the value column. Your casting call should be
| 
| dcast(aa, ... ~ Target, value.var = Eaten)

Thanks for that illumination.  That does exactly what I wanted.  I
knew there were many ways of achieving the result, but I was
particularly interested in understanding how dcast() would do it.
It's also more elegant than any of the other ways of achieving the
same result.

Thanks also for the other suggestions from various other responders.

P


| 
| Best,
| Ista
| 
| On Tue, Jan 22, 2013 at 4:23 AM, Patrick Connolly
| p_conno...@slingshot.co.nz wrote:
|  Suppose I have a small dataframe
| 
|  aa
|   Target Eaten ID
|  50  TPP 0  1
|  51  TPP 1  2
|  52  TPP 3  3
|  53  TPP 1  4
|  54  TPP 2  5
|  50.1GPA 9  1
|  51.1GPA11  2
|  52.1GPA 8  3
|  53.1GPA 8  4
|  54.1GPA10  5
| 
|  And I want to reshape it into
| 
|ID TPP GPA
|  1  1   0   9
|  2  2   1  11
|  3  3   3   8
|  4  4   1   8
|  5  5   2  10
| 
|  I realise that dcast function in the reshape2 package can handle much
|  more complicated tasks than that, but I can't make it do a simple one.
| 
|  If I simply tried
| 
|  dcast(aa, ... ~ Target)
|  Using ID as value column: use value.var to override.
|  Aggregation function missing: defaulting to length
|Eaten GPA TPP
|  1 0   0   1
|  2 1   0   2
|  3 2   0   1
|  4 3   0   1
|  5 8   2   0
|  6 9   1   0
|  710   1   0
|  811   1   0
| 
|  As per the help file, it's giving counts of the numbers in the Eaten
|  column since that's the default fun.aggregate value.
| 
|  My questions are: what fun.aggregate would work?  Alternatively, can
|  value.var be set to something useful?
| 
|  TIA
| 
|  --
|  ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
| ___Patrick Connolly
|   {~._.~}   Great minds discuss ideas
|   _( Y )_ Average minds discuss events
|  (:_~*~_:)  Small minds discuss people
|   (_)-(_)  . Eleanor Roosevelt
| 
|  ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
| 
|  __
|  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.

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~}   Great minds discuss ideas
 _( Y )_ Average minds discuss events 
(:_~*~_:)  Small minds discuss people  
 (_)-(_)  . Eleanor Roosevelt
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
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] Simple use of dcast (reshape2 package)

2013-01-22 Thread Patrick Connolly
Suppose I have a small dataframe

 aa
 Target Eaten ID
50  TPP 0  1
51  TPP 1  2
52  TPP 3  3
53  TPP 1  4
54  TPP 2  5
50.1GPA 9  1
51.1GPA11  2
52.1GPA 8  3
53.1GPA 8  4
54.1GPA10  5

And I want to reshape it into 

  ID TPP GPA
1  1   0   9
2  2   1  11
3  3   3   8
4  4   1   8
5  5   2  10

I realise that dcast function in the reshape2 package can handle much
more complicated tasks than that, but I can't make it do a simple one.

If I simply tried 

 dcast(aa, ... ~ Target)
Using ID as value column: use value.var to override.
Aggregation function missing: defaulting to length
  Eaten GPA TPP
1 0   0   1
2 1   0   2
3 2   0   1
4 3   0   1
5 8   2   0
6 9   1   0
710   1   0
811   1   0

As per the help file, it's giving counts of the numbers in the Eaten
column since that's the default fun.aggregate value.

My questions are: what fun.aggregate would work?  Alternatively, can
value.var be set to something useful?

TIA

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~}   Great minds discuss ideas
 _( Y )_ Average minds discuss events 
(:_~*~_:)  Small minds discuss people  
 (_)-(_)  . Eleanor Roosevelt
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
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] Simple use of dcast (reshape2 package)

2013-01-22 Thread D. Rizopoulos
you could try the following:

DF - read.table(textConnection(
  Target Eaten ID
50  TPP 0  1
51  TPP 1  2
52  TPP 3  3
53  TPP 1  4
54  TPP 2  5
50.1GPA 9  1
51.1GPA11  2
52.1GPA 8  3
53.1GPA 8  4
54.1GPA10  5), header = TRUE)


newDF - as.data.frame(with(DF, tapply(Eaten, list(ID, Target), c)))
newDF$ID - unique(DF$ID)
newDF


I hope it helps.

Best,
Dimitris



On 1/22/2013 10:23 AM, Patrick Connolly wrote:
 Suppose I have a small dataframe

 aa
   Target Eaten ID
 50  TPP 0  1
 51  TPP 1  2
 52  TPP 3  3
 53  TPP 1  4
 54  TPP 2  5
 50.1GPA 9  1
 51.1GPA11  2
 52.1GPA 8  3
 53.1GPA 8  4
 54.1GPA10  5

 And I want to reshape it into

ID TPP GPA
 1  1   0   9
 2  2   1  11
 3  3   3   8
 4  4   1   8
 5  5   2  10

 I realise that dcast function in the reshape2 package can handle much
 more complicated tasks than that, but I can't make it do a simple one.

 If I simply tried

 dcast(aa, ... ~ Target)
 Using ID as value column: use value.var to override.
 Aggregation function missing: defaulting to length
Eaten GPA TPP
 1 0   0   1
 2 1   0   2
 3 2   0   1
 4 3   0   1
 5 8   2   0
 6 9   1   0
 710   1   0
 811   1   0

 As per the help file, it's giving counts of the numbers in the Eaten
 column since that's the default fun.aggregate value.

 My questions are: what fun.aggregate would work?  Alternatively, can
 value.var be set to something useful?

 TIA


-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
__
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] Simple use of dcast (reshape2 package)

2013-01-22 Thread Gerrit Eichner

Hi, Patrick,

I think (with reshape from the stats package)

reshape( aa, idvar = ID, v.names = Eaten, timevar = Target,
 direction = wide)

does the trick (followed by renaming the columns of the resulting data 
frame).


 Hth  --  Gerrit


On Tue, 22 Jan 2013, Patrick Connolly wrote:


Suppose I have a small dataframe


aa

Target Eaten ID
50  TPP 0  1
51  TPP 1  2
52  TPP 3  3
53  TPP 1  4
54  TPP 2  5
50.1GPA 9  1
51.1GPA11  2
52.1GPA 8  3
53.1GPA 8  4
54.1GPA10  5

And I want to reshape it into

 ID TPP GPA
1  1   0   9
2  2   1  11
3  3   3   8
4  4   1   8
5  5   2  10

I realise that dcast function in the reshape2 package can handle much
more complicated tasks than that, but I can't make it do a simple one.

If I simply tried


dcast(aa, ... ~ Target)

Using ID as value column: use value.var to override.
Aggregation function missing: defaulting to length
 Eaten GPA TPP
1 0   0   1
2 1   0   2
3 2   0   1
4 3   0   1
5 8   2   0
6 9   1   0
710   1   0
811   1   0

As per the help file, it's giving counts of the numbers in the Eaten
column since that's the default fun.aggregate value.

My questions are: what fun.aggregate would work?  Alternatively, can
value.var be set to something useful?

TIA

--
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
  ___Patrick Connolly
{~._.~}   Great minds discuss ideas
_( Y )_  Average minds discuss events
(:_~*~_:)  Small minds discuss people
(_)-(_)   . Eleanor Roosevelt

~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
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] Simple use of dcast (reshape2 package)

2013-01-22 Thread Ista Zahn
Hi,

ID is not the value column. Your casting call should be

dcast(aa, ... ~ Target, value.var = Eaten)

Best,
Ista

On Tue, Jan 22, 2013 at 4:23 AM, Patrick Connolly
p_conno...@slingshot.co.nz wrote:
 Suppose I have a small dataframe

 aa
  Target Eaten ID
 50  TPP 0  1
 51  TPP 1  2
 52  TPP 3  3
 53  TPP 1  4
 54  TPP 2  5
 50.1GPA 9  1
 51.1GPA11  2
 52.1GPA 8  3
 53.1GPA 8  4
 54.1GPA10  5

 And I want to reshape it into

   ID TPP GPA
 1  1   0   9
 2  2   1  11
 3  3   3   8
 4  4   1   8
 5  5   2  10

 I realise that dcast function in the reshape2 package can handle much
 more complicated tasks than that, but I can't make it do a simple one.

 If I simply tried

 dcast(aa, ... ~ Target)
 Using ID as value column: use value.var to override.
 Aggregation function missing: defaulting to length
   Eaten GPA TPP
 1 0   0   1
 2 1   0   2
 3 2   0   1
 4 3   0   1
 5 8   2   0
 6 9   1   0
 710   1   0
 811   1   0

 As per the help file, it's giving counts of the numbers in the Eaten
 column since that's the default fun.aggregate value.

 My questions are: what fun.aggregate would work?  Alternatively, can
 value.var be set to something useful?

 TIA

 --
 ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
___Patrick Connolly
  {~._.~}   Great minds discuss ideas
  _( Y )_ Average minds discuss events
 (:_~*~_:)  Small minds discuss people
  (_)-(_)  . Eleanor Roosevelt

 ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

 __
 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] Simple use of dcast (reshape2 package)

2013-01-22 Thread arun
Hi,

This could be done with ?aggregate()
res-aggregate(aa$Eaten,by=list(ID=aa$ID),FUN=function(x) x)
res1-data.frame(ID=res[,1],data.frame(res[[2]]))
 names(res1)[2:3]-unique(aa$Target)
 res1
#  ID TPP GPA
#1  1   0   9
#2  2   1  11
#3  3   3   8
#4  4   1   8
#5  5   2  10
A.K.




- Original Message -
From: Patrick Connolly p_conno...@slingshot.co.nz
To: R-help r-help@r-project.org
Cc: 
Sent: Tuesday, January 22, 2013 4:23 AM
Subject: [R] Simple use of dcast (reshape2 package)

Suppose I have a small dataframe

 aa
     Target Eaten ID
50      TPP     0  1
51      TPP     1  2
52      TPP     3  3
53      TPP     1  4
54      TPP     2  5
50.1    GPA     9  1
51.1    GPA    11  2
52.1    GPA     8  3
53.1    GPA     8  4
54.1    GPA    10  5

And I want to reshape it into 

  ID TPP GPA
1  1   0   9
2  2   1  11
3  3   3   8
4  4   1   8
5  5   2  10

I realise that dcast function in the reshape2 package can handle much
more complicated tasks than that, but I can't make it do a simple one.

If I simply tried 

 dcast(aa, ... ~ Target)
Using ID as value column: use value.var to override.
Aggregation function missing: defaulting to length
  Eaten GPA TPP
1     0   0   1
2     1   0   2
3     2   0   1
4     3   0   1
5     8   2   0
6     9   1   0
7    10   1   0
8    11   1   0

As per the help file, it's giving counts of the numbers in the Eaten
column since that's the default fun.aggregate value.

My questions are: what fun.aggregate would work?  Alternatively, can
value.var be set to something useful?

TIA

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.  
   ___    Patrick Connolly  
{~._.~}                   Great minds discuss ideas    
_( Y )_               Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
(_)-(_)                            . Eleanor Roosevelt
      
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

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