R loading data set from a text into matrix

10,245

Solution 1

Take a look at cast from the reshape package. You can change your data from a "long" format to a "wide" format and back again. There is also native R functions, reshape for example, but I feel the reshape or reshape2 package provide an easier interface.

> dat<-data.frame(stock=letters[1:10], date=rep(1:10,each=10), price=round(runif(100),2))
> head(dat)
  stock date price
1     a    1  0.84
2     b    1  0.46
3     c    1  0.43
4     d    1  0.99
5     e    1  0.86
6     f    1  0.03
> cast(date~stock, data=dat, value="price")
   date    a    b    c    d    e    f    g    h    i    j
1     1 0.84 0.46 0.43 0.99 0.86 0.03 0.56 0.09 0.66 0.43
2     2 0.46 0.78 0.80 0.90 0.20 0.87 0.73 0.65 0.35 0.24
3     3 0.12 0.16 0.57 0.10 0.40 0.05 0.68 0.53 0.67 0.74
4     4 0.45 0.26 0.63 0.98 0.78 0.29 0.59 0.63 0.62 0.13
5     5 0.21 0.79 0.83 0.36 0.11 0.12 0.64 0.86 0.35 0.18
6     6 0.14 0.31 0.63 0.48 0.86 0.57 0.32 0.70 0.89 0.99
7     7 0.85 0.41 0.29 0.57 0.05 0.80 0.32 0.13 0.10 0.53
8     8 0.03 0.71 0.48 0.57 0.09 0.88 0.70 0.22 0.68 0.78
9     9 0.83 0.52 0.24 0.82 0.86 0.87 0.53 0.38 0.58 0.78
10   10 0.36 0.05 0.95 0.41 0.73 0.20 0.62 0.08 0.98 0.50
> 

Solution 2

read.zoo in the zoo package can do that. Assume this data:

Lines <- "N     stock  date price
1        S1 70516  9.11
2        S1 70517  9.00
5718864  S5000 100330  64.8
5718865  S5000 100331  64.6"

Then we create a function to convert the date (change this if the coding of the dates is different than assumed here) and use read.zoo specifying

  • a header, header = TRUE
  • to split on the stock column, split = 1
  • to use the indicated column as the time index, index = 2
  • to use the indicated colClasses to exclude the first column (so the second column becomes the first, the third becomes the second, etc.) as that column seems to be unneeded, , colClasses = c("NULL", NA, NA, NA)
  • if the data actually comes from a file rather than from text then replace text = Lines with something like file = "myfile.dat"

giving:

library(zoo)

toDate <- function(x) as.Date(sprintf("%06d", x), "%y%m%d")

z <- read.zoo(text = Lines, header = TRUE, split = 1, index = 2, 
   FUN = toDate, colClasses = c("NULL", NA, NA, NA))

The result is the following zoo object:

> z
             S1 S5000
2007-05-16 9.11    NA
2007-05-17 9.00    NA
2010-03-30   NA  64.8
2010-03-31   NA  64.6

Solution 3

Use as.matrix(). See help(as.matrix).

Share:
10,245
pyCthon
Author by

pyCthon

Updated on June 17, 2022

Comments

  • pyCthon
    pyCthon almost 2 years

    I have a data set from a text file, 5000 stock's with 751 data points for 3 years worth of data, it goes from day 1 to 751 then the next stock

        N     stock  date price
        1        S1 70516  9.11
        2        S1 70517  9.00
        .......................
        5718864  S5000 100330  64.8
        5718865  S5000 100331  64.6
    

    I can load the data using:

    data(txt) <- read.table("C:\\test.txt", header=T)
    

    after this however im stuck and i cant find anything on the exchange or google how can i load this into a matrix where there are 751 rows and 5000 coloumns and each point corresponds to just the price

        E1 price     E5000 price
    day1   1 ........  1
    ..                 ..
    ..                 ..
    day751 ........... 1