简体   繁体   English

将向量作为单个观测值添加到data.frame

[英]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. 我正在尝试在data.frame中保存许多光谱测量值。 Each measurement has a number of attributes as well as two channels of spectral data, each with 2048 data points. 每次测量都具有许多属性以及两个光谱数据通道,每个通道具有2048个数据点。 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. 其中每个(spec)是2048个值的向量。 I simply cannot get it to work, and I now turn to you guys for help. 我根本无法正常工作,现在我向你们寻求帮助。

Thanks in advance. 提前致谢。

You can add matrix as one of data.frame fields, so you have to put all vectors as matrix rows. 您可以将矩阵添加为data.frame字段之一,因此必须将所有向量作为矩阵行。

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

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. 但请注意Joris的建议,因为这可能是存储数据的更好方法。 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. 通过将2048个值的向量存储在列表中,然后将其作为列添加到数据框中,可以完成所需的操作。 Your table wasn't easily imported (for me anyway) with read.table so I made up my own data frame and example. 您的表很难通过read.table导入(无论如何对我来说),因此我组成了自己的数据框和示例。

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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM