[R] Data frame transpose

2003-09-29 Thread Kang . Daiwen




Hi All,

I want to ask if there is a transpose function for data frame like the
procedure of transpose in SAS? Because I want to partially transpose a
data frame which contains 5 columns (siteid, date, time, obs, mod), what
I want to do is to put time as the column variables along with siteid,
and date, and put obs and mod in the row names. specifically to
transpose a data frame:

siteid datetime   obs mod
A   7/801   2 5
A   7/80238
A   7/80358
 A   7/9   01   3  6
   A 7/9  025  8
   A 7/9   03   6   7
   ..
   B 7/8  01  4 7
B7/8  02719
   B 7/8 03 49
  ..

To

siteiddatename 0102 03   
A  7/8   obs   2  35
A  7/8   mod  5  88
A  7/9   obs   3  5 6
A  7/9   mod  6  87
 ...
B  7/8obs   4  74
B  7/8mod  7  19  9



Thank you very much!

Dave

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Data frame transpose

2003-09-29 Thread Thomas Lumley
On Mon, 29 Sep 2003 [EMAIL PROTECTED] wrote:

 Hi All,

 I want to ask if there is a transpose function for data frame like the
 procedure of transpose in SAS? Because I want to partially transpose a
 data frame which contains 5 columns (siteid, date, time, obs, mod), what
 I want to do is to put time as the column variables along with siteid,
 and date, and put obs and mod in the row names. specifically to

I think reshape() does what you want

-thomas


 transpose a data frame:

 siteid datetime   obs mod
 A   7/801   2 5
 A   7/80238
 A   7/80358
  A   7/9   01   3  6
A 7/9  025  8
A 7/9   03   6   7
..
B 7/8  01  4 7
 B7/8  02719
B 7/8 03 49
   ..

 To

 siteiddatename 0102 03   
 A  7/8   obs   2  35
 A  7/8   mod  5  88
 A  7/9   obs   3  5 6
 A  7/9   mod  6  87
  ...
 B  7/8obs   4  74
 B  7/8mod  7  19  9
 


 Thank you very much!

 Dave

 __
 [EMAIL PROTECTED] mailing list
 https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Thomas Lumley   Assoc. Professor, Biostatistics
[EMAIL PROTECTED]   University of Washington, Seattle

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Data frame transpose

2003-09-29 Thread Thomas W Blackwell
Dave  -

I'm not sure whether there is already a function which does
exactly what you want, because this is kind of a special case.
The functions I wold look at are: by, aggregate, tapply,
mapply, and, in the package nlme one I didn't know about
before called gapply.

But, in your case, the part that makes it doable is that all
of the columns in that part of the dataframe which you want
to transpose are numeric.  (At least, it looks that way from
your email.)  And you have the same number of time points in
every site x date.  So you could extract just the three columns
time, obs, mod as a numeric matrix, cast it to an array,
use  aperm(), then cast it back to a matrix and then back to a
dataframe.  The basic tools for this are: data.matrix, array,
aperm, matrix and data.frame.

I've got to let you work out the details yourself.  The key
when planning this is to remember that multi-dimensional arrays
in R use Fortran storage order: the first index varies fastest.
Some one else may be able to come up with a much more direct
solution.

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Mon, 29 Sep 2003 [EMAIL PROTECTED] wrote:

 I want to ask if there is a transpose function for data frame like the
 procedure of transpose in SAS? Because I want to partially transpose a
 data frame which contains 5 columns (siteid, date, time, obs, mod), what
 I want to do is to put time as the column variables along with siteid,
 and date, and put obs and mod in the row names. specifically to
 transpose a data frame:

 siteid datetime   obs mod
 A   7/801   2 5
 A   7/80238
 A   7/80358
  A   7/9   01   3  6
A 7/9  025  8
A 7/9   03   6   7
..
B 7/8  01  4 7
 B7/8  02719
B 7/8 03 49
   ..
 To

 siteiddatename 0102 03   
 A  7/8   obs   2  35
 A  7/8   mod  5  88
 A  7/9   obs   3  5 6
 A  7/9   mod  6  87
  ...
 B  7/8obs   4  74
 B  7/8mod  7  19  9
 

 Thank you very much!

 Dave

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


RE: [R] Data frame transpose

2003-09-29 Thread Gabor Grothendieck
 Assuming that the data frame is called m, you can - split the data frame by the 
values of siteid and date- apply a function to create the new data frame for each such 
group and - use rbind to put it back together like this: fn lt;- function(x) { y 
lt;- t(x[,4:5]) data.frame( siteid=x[1,1], date=x[1,2], name=colnames(x)[4:5], 
x01=y[,1], x02=y[,2], x03=y[,3] )}m lt;- do.call( rbind, 
lapply(split(m,list(m$siteid,m$date)),fn) )If the order matters then add this line: m 
lt;- m[ order(m$siteid,m$date), ]--- On Mon 09/29, lt; [EMAIL PROTECTED] gt; 
wrote:From: [mailto: [EMAIL PROTECTED]: [EMAIL PROTECTED]: Mon, 29 Sep 2003 13:15:36 
-0400Subject: [R] Data frame transposeHi All,I want to ask if there is a transpose 
function for data frame like theprocedure of transpose in SAS? Because I want to 
partially transpose adata frame which contains 5 columns (siteid, date, time, obs, 
mod), whatI want to do is to put time as the column variables along with siteid,and 
date, and put obs and mod in the row names. specifically totranspose a data 
frame:siteid date time obs modA 7/8 01 2 5A 7/8 02 3 8A 7/8 03 5 8A 7/9 01 3 6A 7/9 02 
5 8A 7/9 03 6 7..B 7/8 01 4 7B 7/8 02 7 19B 7/8 03 4 9..Tositeid date name 01 
02 03 A 7/8 obs 2 3 5A 7/8 mod 5 8 8A 7/9 obs 3 5 6A 7/9 mod 6 8 7...B 7/8 obs 
4 7 4B 7/8 mod 7 19 9Thank you very [EMAIL PROTECTED] mailing 
listhttps://www.stat.math.ethz.ch/mailman/listinfo/r-help

___
No banners. No pop-ups. No kidding.
Introducing My Way - http://www.myway.com

[[alternative HTML version deleted]]

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


RE: [R] Data frame transpose

2003-09-29 Thread Gabor Grothendieck


Sorry about the formatting in that previous email.  Here it is
again, hopefully with the correct formatting this time.

Assuming that the data frame is called m, you can 
- split the data frame by the values of siteid and date
- apply a function to create the new data frame for each such group and 
- use rbind to put it back together like this: 

fn - function(x) {
  y - t(x[,4:5])
  data.frame( siteid=x[1,1], date=x[1,2], name=colnames(x)[4:5], x01=y[,1], x02=y[,2], 
x03=y[,3] )
 }
m - do.call( rbind, lapply(split(m,list(m$siteid,m$date)),fn) )

If order matters, add this:

m - m[order(m$siteid,m$date),]





 --- On Mon 09/29,   [EMAIL PROTECTED]  wrote:
From:  [mailto: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Date: Mon, 29 Sep 2003 13:15:36 -0400
Subject: [R] Data frame transpose

brbrbrbrHi All,brbrI want to ask if there is a transpose function for data 
frame like thebrprocedure of transpose in SAS? Because I want to partially transpose 
abrdata frame which contains 5 columns (siteid, date, time, obs, mod), whatbrI 
want to do is to put time as the column variables along with siteid,brand date, and 
put obs and mod in the row names. specifically tobrtranspose a data 
frame:brbrsiteid datetime   obs modbrA   7/801   
2 5brA   7/80238brA   7/8
0358br A   7/9   01   3  6br   A 
7/9  025  8br   A 7/9   03   6   7br   
..br   B 7/8  01  4 7brB   
 7/8  02719br   B 7/8 03 49br  
..brbrTobrbrsiteiddate!
 name 0102 03   brA  7/8   obs   2  3
5brA  7/8   mod  5  88brA  7/9 
  obs   3  5 6brA  7/9   mod  6  8
7br ...brB  7/8obs   4  7
4brB  7/8mod  7  19  9br
brbrbrThank you very 
much!brbrDavebrbr__br[EMAIL 
PROTECTED] mailing listbrhttps://www.stat.math.ethz.ch/mailman/listinfo/r-helpbr

___
No banners. No pop-ups. No kidding.
Introducing My Way - http://www.myway.com

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help