Move the data of the row by factors in the columns of R
source link: https://www.codesd.com/item/move-the-data-of-the-row-by-factors-in-the-columns-of-r.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Move the data of the row by factors in the columns of R
I have a dataset as follows:
> head(data)
Symbol Date Close
1 A 2009-01-01 15.63
2 AA 2009-01-01 11.26
3 AAP 2009-01-01 33.65
4 AAV 2009-01-01 4.21
5 AB 2009-01-01 20.79
6 ABB 2009-01-01 15.01
There are about 2500 uniques in data$Symbol. What I need to convert this to is something like the following, where the dates stay on the left but each symbol has its separate column. Note that some symbols will have some dates without data.
Date A AA AAP AAV.....
1 2009-01-01 15.63 11.26 33.65 4.21...
2 2009-01-02 15.26 11.25 33.62 3.88
3 2009-01-03 16.23 11.05 34.63 4.05
4 2009-01-04 16.21 11.72 35.19 4.35
...and so on
I have tried a number of things, finally deciding to move each data into a separate data frame using the split function, then converting each to an xts object from where I would use the xts merge function. The problem I am facing is that I am trying to use strings as variable names, but it is not working, though I have tried get, assign, all I get is an error. My code is as follows:
t1 <- unique(data$Symbol)
head(t1)
blocks <- split(data,data$Symbol)
for (i in 1:length(t1) ) {
symb= t1[i]
zclose <-paste("blocks$",symb,"$Close",sep="")
zdate <- paste("blocks$",symb,"$Date",sep="")
assign(paste("DataFor",symb,sep=""), xts(zclose,zdate))
}
The last part errors out with "Error in xts(zclose, zdate) : order.by requires an appropriate time-based object".
It works if I were to hard code the names, eg, blocks$AA$Close and blocks$AA$Date; but does not if these variable names are contained as strings in zclose and zdate respectively.
Many thanks for reading if you are with me thus far. Am fairly new to R, so would appreciate any pointers.
You can use reshape2
package to convert your data from long format to wide format. Then convert it into xts
require(reshape2)
require(xts)
data
## Date Symbol Close
## 1 2009-01-01 A 15.63
## 2 2009-01-02 A 15.26
## 3 2009-01-03 A 16.23
## 4 2009-01-04 A 16.21
## 5 2009-01-01 AA 11.26
## 6 2009-01-02 AA 11.25
## 7 2009-01-03 AA 11.05
## 8 2009-01-04 AA 11.72
## 9 2009-01-01 AAP 33.65
## 10 2009-01-02 AAP 33.62
## 11 2009-01-03 AAP 34.63
## 12 2009-01-04 AAP 35.19
## 13 2009-01-01 AAV 4.21
## 14 2009-01-02 AAV 3.88
## 15 2009-01-03 AAV 4.05
## 16 2009-01-04 AAV 4.35
tmp <- dcast(data = data, formula = Date ~ Symbol)
Using Close as value column: use value.var to override.
RES <- xts(x = tmp[, -1], order.by = as.Date(tmp[, 1]))
RES
## A AA AAP AAV
## 2009-01-01 15.63 11.26 33.65 4.21
## 2009-01-02 15.26 11.25 33.62 3.88
## 2009-01-03 16.23 11.05 34.63 4.05
## 2009-01-04 16.21 11.72 35.19 4.35
str(RES)
## An ‘xts’ object on 2009-01-01/2009-01-04 containing:
## Data: num [1:4, 1:4] 15.6 15.3 16.2 16.2 11.3 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:4] "A" "AA" "AAP" "AAV"
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK