Re: [R] reorganize data

2013-02-12 Thread David Winsemius

On Feb 12, 2013, at 2:56 AM, Niklas Larson wrote:

 Hi R users,
 Wonder if somebody could give me help on how to reshape this type of data:
 
#Adding a bit of code to read this into R:
Lines - readLines(textConnection(Date:10.09.19 Time:21:39:05  Lat:N62.37.18 
Long:E018.07.32
-0010 |   28|   28
0010-0020|  302|  302
0020-0030|   42|   42
0030-0040|2|2
0040-0050|1|1
0060-0070|1|1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07
-0010|   32|   32
0010-0020|  334|  334
0020-0030|   27|   27
0030-0040|2|2
0070-0080|1|1
0080-0090|1|1
0090-0100|1|1
0100-0110|1|1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40
-0010|   63|   63
0010-0020|  303|  303
0020-0030|   85|   85
0030-0040|   12|   12
0040-0050|4|4
0050-0060|1|1
0060-0070|4|4
0070-0080|1|1))


 
 --
 
 into this:
 
 -
 Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 -0010|   28|
  28
 Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0010-0020|  302|
 302
 Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0020-0030|   42|
  42
 
 Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0070-0080|1|
   1
 
Snipped

This should get you started along one possible path. Still to be done would be 
removing the lines that only had dashes and perhaps parsing the headers.

 data.frame(dattimeLatlon = Lines[rep( grep(Date, Lines), each=diff(numdt))] ,
  vals=Lines[ grep(^[^D], Lines) ] )
#--
dattimeLatlon   
vals
1  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32 -0010 |   28| 
  28
2  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0010-0020|  302| 
 302
3  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0020-0030|   42| 
  42
4  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0030-0040|2| 
   2
5  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0040-0050|1| 
   1
6  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0060-0070|1| 
   1
7  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32   
   _
8  Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  -0010|   32| 
  32
9   Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0010-0020|  334| 
 334
10  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0020-0030|   27| 
  27
11  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0030-0040|2| 
   2
12  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0070-0080|1| 
   1
13  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0080-0090|1| 
   1
14  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0090-0100|1| 
   1
15  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07  0100-0110|1| 
   1
16  Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07   
   _
17  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  -0010|   63| 
  63
18  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0010-0020|  303| 
 303
19  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0020-0030|   85| 
  85
20  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0030-0040|   12| 
  12
21  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0040-0050|4| 
   4
22  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0050-0060|1| 
   1
23  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0060-0070|4| 
   4
24  Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40  0070-0080|1| 
   1

David Winsemius
Alameda, CA, USA

__
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] reorganize data

2013-02-12 Thread arun
Hi,


Lines1 - readLines(textConnection(Date:10.09.19 Time:21:39:05  Lat:N62.37.18 
Long:E018.07.32
-0010 |   28|   28
0010-0020|  302|  302
0020-0030|   42|   42
0030-0040|    2|    2
0040-0050|    1|    1
0060-0070|    1|    1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07
-0010|   32|   32
0010-0020|  334|  334
0020-0030|   27|   27
0030-0040|    2|    2
0070-0080|    1|    1
0080-0090|    1|    1
0090-0100|    1|    1
0100-0110|    1|    1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40
-0010|   63|   63
0010-0020|  303|  303
0020-0030|   85|   85
0030-0040|   12|   12
0040-0050|    4|    4
0050-0060|    1|    1
0060-0070|    4|    4
0070-0080|    1|    1)) 
Lines2- Lines1[Lines1!=_]
indx- grep(Date,Lines2)
 dat1- data.frame( 
Lines2[rep(indx,diff(c(indx,length(Lines2)+1))-1)],read.table(text=gsub(^\\s+ 
,,gsub(\\|,,gsub(^\\d+-\\d+|\\s+(.*),\\1,Lines2[-indx]))),sep=,header=FALSE),stringsAsFactors=FALSE)
 names(dat1)[1]- Date_Lat_Long
head(dat1)
   Date_Lat_Long  V1  V2
#1 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  28  28
#2 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32 302 302
#3 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  42  42
#4 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32   2   2
#5 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32   1   1
#6 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32   1   1


A.K.

- Original Message -
From: Niklas Larson nik...@gmail.com
To: r-help@r-project.org
Cc: 
Sent: Tuesday, February 12, 2013 5:56 AM
Subject: [R] reorganize data

Hi R users,
Wonder if somebody could give me help on how to reshape this type of data:

---
Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32
-0010 |   28|   28
0010-0020|  302|  302
0020-0030|   42|   42
0030-0040|    2|    2
0040-0050|    1|    1
0060-0070|    1|    1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07
-0010|   32|   32
0010-0020|  334|  334
0020-0030|   27|   27
0030-0040|    2|    2
0070-0080|    1|    1
0080-0090|    1|    1
0090-0100|    1|    1
0100-0110|    1|    1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40
-0010|   63|   63
0010-0020|  303|  303
0020-0030|   85|   85
0030-0040|   12|   12
0040-0050|    4|    4
0050-0060|    1|    1
0060-0070|    4|    4
0070-0080|    1|    1

--

into this:

-
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 -0010|   28|
  28
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0010-0020|  302|
302
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0020-0030|   42|
  42
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0030-0040|    2|
   2
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0040-0050|    1|
   1
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0060-0070|    1|
   1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 -0010|   32|
  32
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0010-0020|  334|
334
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0020-0030|   27|
  27
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0030-0040|    2|
   2
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0070-0080|    1|
   1
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0080-0090|    1|
   1
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0090-0100|    1|
   1
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0100-0110|    1|
   1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 -0010|   63|
  63
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0010-0020|  303|
303
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0020-0030|   85|
  85
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0030-0040|   12|
  12
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0040-0050|    4|
   4
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0050-0060|    1|
   1
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0060-0070|    4|
   4
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0070-0080|    1|
   1

-

Thanx

/Nic

    [[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] reorganize data

2013-02-12 Thread arun
Hi,

Sorry, in my previous reply, I omitted one of the columns.
Lines1 - readLines(textConnection(Date:10.09.19 Time:21:39:05  Lat:N62.37.18 
Long:E018.07.32
-0010 |   28|   28
0010-0020|  302|  302
0020-0030|   42|   42
0030-0040|    2|    2
0040-0050|    1|    1
0060-0070|    1|    1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07
-0010|   32|   32
0010-0020|  334|  334
0020-0030|   27|   27
0030-0040|    2|    2
0070-0080|    1|    1
0080-0090|    1|    1
0090-0100|    1|    1
0100-0110|    1|    1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40
-0010|   63|   63
0010-0020|  303|  303
0020-0030|   85|   85
0030-0040|   12|   12
0040-0050|    4|    4
0050-0060|    1|    1
0060-0070|    4|    4
0070-0080|    1|    1)) 
Lines2- Lines1[Lines1!=_]  
indx- grep(Date,Lines2)
#If you need the `values` as separate columns

dat1-data.frame(Lines2[rep(indx,diff(c(indx,length(Lines2)+1))-1)],read.table(text=Lines2[-indx],sep=|,stringsAsFactors=FALSE),stringsAsFactors=FALSE)
 names(dat1)[1]- Date_Lat_Long
head(dat1,3)
#   Date_Lat_Long V1  V2  V3
#1 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32 -0010   28  28
#2 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0010-0020 302 302
#3 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32  0020-0030  42  42
#Or  need it in the format as specified by you:

dat2- 
data.frame(Date_Lat_Long=Lines2[rep(indx,diff(c(indx,length(Lines2)+1))-1)],val=Lines2[-indx],stringsAsFactors=FALSE)
head(dat2,3)
#   Date_Lat_Long
#1 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32
#2 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32
#3 Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32
# val
#1 -0010 |   28|   28
#2  0010-0020|  302|  302
#3  0020-0030|   42|   42
A.K.






- Original Message -
From: Niklas Larson nik...@gmail.com
To: r-help@r-project.org
Cc: 
Sent: Tuesday, February 12, 2013 5:56 AM
Subject: [R] reorganize data

Hi R users,
Wonder if somebody could give me help on how to reshape this type of data:

---
Date:10.09.19 Time:21:39:05  Lat:N62.37.18 Long:E018.07.32
-0010 |   28|   28
0010-0020|  302|  302
0020-0030|   42|   42
0030-0040|    2|    2
0040-0050|    1|    1
0060-0070|    1|    1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07
-0010|   32|   32
0010-0020|  334|  334
0020-0030|   27|   27
0030-0040|    2|    2
0070-0080|    1|    1
0080-0090|    1|    1
0090-0100|    1|    1
0100-0110|    1|    1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40
-0010|   63|   63
0010-0020|  303|  303
0020-0030|   85|   85
0030-0040|   12|   12
0040-0050|    4|    4
0050-0060|    1|    1
0060-0070|    4|    4
0070-0080|    1|    1

--

into this:

-
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 -0010|   28|
  28
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0010-0020|  302|
302
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0020-0030|   42|
  42
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0030-0040|    2|
   2
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0040-0050|    1|
   1
Date:10.09.19 Time:21:39:05 Lat:N62.37.18 Long:E018.07.32 0060-0070|    1|
   1
_
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 -0010|   32|
  32
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0010-0020|  334|
334
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0020-0030|   27|
  27
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0030-0040|    2|
   2
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0070-0080|    1|
   1
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0080-0090|    1|
   1
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0090-0100|    1|
   1
Date:10.09.19 Time:21:44:52 Lat:N62.38.00 Long:E018.09.07 0100-0110|    1|
   1
_
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 -0010|   63|
  63
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0010-0020|  303|
303
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0020-0030|   85|
  85
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0030-0040|   12|
  12
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0040-0050|    4|
   4
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0050-0060|    1|
   1
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0060-0070|    4|
   4
Date:10.09.19 Time:21:50:35 Lat:N62.38.42 Long:E018.10.40 0070-0080|    1|
   1


Re: [R] Reorganize data fram

2011-07-15 Thread anglor
Hi, thanks for your reply. 

I didn't get cast() to work and didn't know how to find information about it
either. I used reshape but then I had to subset only those columns (actually
I have 28 columns of other data) Could cast or reshape work also with more
columns?

Angelica



--
View this message in context: 
http://r.789695.n4.nabble.com/Reorganize-data-fram-tp3662123p3669899.html
Sent from the R help mailing list archive at Nabble.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] Reorganize data fram

2011-07-15 Thread anglor
Thank you!

I used this one and it worked really great.



/Angelica

--
View this message in context: 
http://r.789695.n4.nabble.com/Reorganize-data-fram-tp3662123p3669782.html
Sent from the R help mailing list archive at Nabble.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] Reorganize data fram

2011-07-15 Thread Dennis Murphy
Hi:

On Fri, Jul 15, 2011 at 6:04 AM, anglor angelica.ekens...@dpes.gu.se wrote:
 Hi, thanks for your reply.

 I didn't get cast() to work and didn't know how to find information about it
 either.

 Hadley Wickham's home page http://had.co.nz/ has a link (last one
under the heading 'R Packages') to the reshape package page, where
some documentation resides.

Could you post a representative sample of your input data frame using
dput()? Here's an example:
df - data.frame(x = 1:4, y = rnorm(4), z = rpois(4, 3))
 dput(df)
structure(list(x = 1:4, y = c(-0.49491054748322, -1.53013240418216,
0.0189088048735591, -0.0766510981813545), z = c(2, 2, 3, 4)), .Names = c(x,
y, z), row.names = c(NA, -4L), class = data.frame)

Copy and paste the result of dput() into your e-mail. This is the
preferred way to transport data that is readable on all platforms
while guaranteeing that a potential R-helper sees the same data
structure you do.

Clearly, you don't want to send 700,000 observations with dput(), but
a small sample that is sufficient to illustrate the problem is
desirable. If possible, also send the code that you tried and the
expected result, as you did in your initial post.

 I used reshape but then I had to subset only those columns (actually
 I have 28 columns of other data) Could cast or reshape work also with more
 columns?

Are these columns 'constant' apart from Temperature? If so, then the
following should work, but this needs the 'new and improved' reshape2
package instead. I'm using the same data frame d as before with a
couple added 'constant' variables:

d$age - 12
d$region - 'NW'
d$zone - 'CET'
d
 Date Temperature Category age region zone
1 2007102  16A  12 NW  CET
2 2007102  17B  12 NW  CET
3 2007102  18C  12 NW  CET

library(reshape2)
dcast(d, ... ~ Category, value_var = 'Temperature')
 Date age region zone  A  B  C
1 2007102  12 NW  CET 16 17 18

If they're not (all) constant, then you need to post some data per
above and describe your desired outcome.

HTH,
Dennis


 Angelica



 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Reorganize-data-fram-tp3662123p3669899.html
 Sent from the R help mailing list archive at Nabble.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.


__
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] Reorganize data fram

2011-07-12 Thread David Winsemius


On Jul 12, 2011, at 8:42 AM, anglor wrote:


Hi,

I have a data frame of about 700 000 rows which look something like  
this:


DateTemperature  Category
2007102   16   A
2007102   17   B
2007102   18   C

but need it to be:

Date  TemperatureA  TemperatureB   TemperatureC
2007102   16 1718


 reshape(dat, idvar=Date, timevar=Category, direction=wide)
 Date Temperature.A Temperature.B Temperature.C
1 2007102161718




Any suggestions?

/Angelica

--
View this message in context: 
http://r.789695.n4.nabble.com/Reorganize-data-fram-tp3662123p3662123.html
Sent from the R help mailing list archive at Nabble.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.


David Winsemius, MD
West Hartford, CT

__
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] Reorganize data fram

2011-07-12 Thread Dennis Murphy
Hi:

Try the cast() function in the reshape package. Using d as the name of
your data frame,

library(reshape)
cast(d, Date ~ Category, value = 'Temperature')
 Date  A  B  C
1 2007102 16 17 18

HTH,
Dennis


On Tue, Jul 12, 2011 at 5:42 AM, anglor angelica.ekens...@dpes.gu.se wrote:
 Hi,

 I have a data frame of about 700 000 rows which look something like this:

 Date        Temperature  Category
 2007102   16                   A
 2007102   17                   B
 2007102   18                   C

 but need it to be:

 Date          TemperatureA  TemperatureB   TemperatureC
 2007102       16                 17                    18

 Any suggestions?

 /Angelica

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Reorganize-data-fram-tp3662123p3662123.html
 Sent from the R help mailing list archive at Nabble.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.


__
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] Reorganize data frame

2011-03-16 Thread Phil Spector

Here's one way:


 ans = reshape(stock.returns,idvar='Date',

+varying=list(names(stock.returns)[-1]),
+direction='long',
+times=names(stock.returns)[-1],
+v.names='Return',timevar='Ticker')

rownames(ans) = NULL
ans

  Date Ticker Return
1 20110301   MSFT   0.05
2 20110302   MSFT   0.01
3 20110301   GOOG  -0.01
4 20110302   GOOG   0.04

- Phil Spector
 Statistical Computing Facility
 Department of Statistics
 UC Berkeley
 spec...@stat.berkeley.edu



On Wed, 16 Mar 2011, chris99 wrote:


Hi group,

I am trying to convert the organization of a data frame so I can do some
correlations between stocks,

I have something like this:

stock.returns -
data.frame(rbind(c(MSFT,20110301,0.05),c(MSFT,20110302,0.01),c(GOOG,20110301,-0.01),c(GOOG,20110302,0.04)))
colnames(stock.returns) - c(Ticker,Date,Return)
stock.returns
 Ticker Date Return
1   MSFT 20110301   0.05
2   MSFT 20110302   0.01
3   GOOG 20110301  -0.01
4   GOOG 20110302   0.04


And want to convert it to this:

stock.returns -
data.frame(rbind(c(20110301,0.05,-0.01),c(20110302,0.01,0.04)))
colnames(stock.returns) - c(Date,MSFT,GOOG)
stock.returns
 Date MSFT  GOOG
1 20110301 0.05 -0.01
2 20110302 0.01  0.04


Can anyone offer any suggestions?

Thanks,
Chris

--
View this message in context: 
http://r.789695.n4.nabble.com/Reorganize-data-frame-tp3381929p3381929.html
Sent from the R help mailing list archive at Nabble.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.



__
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] Reorganize data frame

2011-03-16 Thread Henrique Dallazuanna
Try this:

xtabs(Return ~ Date + Ticker, stock.returns)

On Wed, Mar 16, 2011 at 11:37 AM, chris99 chea...@hotmail.com wrote:
 Hi group,

 I am trying to convert the organization of a data frame so I can do some
 correlations between stocks,

 I have something like this:

 stock.returns -
 data.frame(rbind(c(MSFT,20110301,0.05),c(MSFT,20110302,0.01),c(GOOG,20110301,-0.01),c(GOOG,20110302,0.04)))
 colnames(stock.returns) - c(Ticker,Date,Return)
 stock.returns
  Ticker     Date Return
 1   MSFT 20110301   0.05
 2   MSFT 20110302   0.01
 3   GOOG 20110301  -0.01
 4   GOOG 20110302   0.04


 And want to convert it to this:

 stock.returns -
 data.frame(rbind(c(20110301,0.05,-0.01),c(20110302,0.01,0.04)))
 colnames(stock.returns) - c(Date,MSFT,GOOG)
 stock.returns
      Date MSFT  GOOG
 1 20110301 0.05 -0.01
 2 20110302 0.01  0.04


 Can anyone offer any suggestions?

 Thanks,
 Chris

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Reorganize-data-frame-tp3381929p3381929.html
 Sent from the R help mailing list archive at Nabble.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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
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] Reorganize data frame

2011-03-16 Thread Scott Chamberlain
require(reshape2)
dcast(stock.returns, Date ~ Ticker)

The numbers were changed from their original values, but if you originally 
created the values Return as.numeric they should stay the same 
On Wednesday, March 16, 2011 at 9:37 AM, chris99 wrote:
Hi group,
 
 I am trying to convert the organization of a data frame so I can do some
 correlations between stocks,
 
 I have something like this:
 
 stock.returns -
 data.frame(rbind(c(MSFT,20110301,0.05),c(MSFT,20110302,0.01),c(GOOG,20110301,-0.01),c(GOOG,20110302,0.04)))
 colnames(stock.returns) - c(Ticker,Date,Return)
 stock.returns
  Ticker Date Return
 1 MSFT 20110301 0.05
 2 MSFT 20110302 0.01
 3 GOOG 20110301 -0.01
 4 GOOG 20110302 0.04
 
 
 And want to convert it to this:
 
 stock.returns -
 data.frame(rbind(c(20110301,0.05,-0.01),c(20110302,0.01,0.04)))
 colnames(stock.returns) - c(Date,MSFT,GOOG)
 stock.returns
  Date MSFT GOOG
 1 20110301 0.05 -0.01
 2 20110302 0.01 0.04
 
 
 Can anyone offer any suggestions?
 
 Thanks,
 Chris
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Reorganize-data-frame-tp3381929p3381929.html
 Sent from the R help mailing list archive at Nabble.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.
 

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