Bjørn Madsen
Bjørn Madsen

Reputation: 388

Add a vector as a single observation to a data.frame

I'm trying to save a number of spectral measurements in a data.frame. Each measurement has a number of attributes as well as two channels of spectral data, each with 2048 data points. I would like to have each channel be a single point of data in the data frame.

Something like this:

  timestamp           type integration channel1 channel2
1 2011-10-02 02:00:01 D    2000        (spec)   (spec)
2 2011-10-02 02:00:07 D    2000        (spec)   (spec)

Where each (spec) is a vector of 2048 values. I simply cannot get it to work, and I now turn to you guys for help.

Thanks in advance.

Upvotes: 0

Views: 1567

Answers (2)

Wojciech Sobala
Wojciech Sobala

Reputation: 7551

You can add matrix as one of data.frame fields, so you have to put all vectors as matrix rows.

DF <- data.frame(timestamp=1:3, type=LETTERS[1:3], integration=rep(2000, 3))
DF$channel1 <- matrix(rnorm(3*2048), nrow=3)
DF$channel2 <- matrix(rnorm(3*2048), nrow=3)
ncol(DF)# == 5

Upvotes: 2

Tyler Rinker
Tyler Rinker

Reputation: 109844

I think what you want is doable but I may not be fully understanding your question. Heed Joris's suggestion though as this may be a better way of storing your data. You can accomplish what you want by storing the vectors of 2048 values in a list that you then add to the data frame as a column. Your table wasn't easily imported (for me anyway) with read.table so I made up my own data frame and example.

DF <- data.frame(timestamp=1:3, type=LETTERS[1:3], integration=rep(2000, 3))
DF$channel1 <- list(c(rnorm(2048)), c(rnorm(2048)), c(rnorm(2048)))
DF$channel2 <- list(c(rnorm(2048)), c(rnorm(2048)), c(rnorm(2048)))

Upvotes: 1

Related Questions