进行以下转换的最佳方法是什么?这种转换有两个部分。第一个是将速度转换为每秒平均值。第二种是将分类列转换为多列——每个分类值一列,其中值是每秒出现的次数。例如:
输入(xts A):
Time(PosixCT), Observed Letter, Speed
2011/01/11 12:12:01.100,A,1
2011/01/11 12:12:01.200,A,2
2011/01/11 12:12:01.400,B,3
2011/01/11 12:12:01.800,C,4
2011/01/11 12:12:02.200,D,2
2011/01/11 12:12:02.200,A,7
输出:(xts B)
Time, A_Per_Second, B_Per_Second, C_Per_Second, D_Per_Second, Aggregate_Speed
2011/01/11 12:12:01,2,1,1,0,2.5
2011/01/11 12:12:02,1,0,0,1,4.5
我希望以不需要知道所有类别的方式来执行此操作。基本上,我试图在不丢失任何分类数据的情况下将时间折叠到每秒,并将数字数据汇总为每秒平均值。
最佳答案
这是我用于 A
的结构。请注意,“数字”实际上是字符,因为您不能在矩阵中混合类型。
A <- structure(c("A", "A", "B", "C", "D", "A", "1", "2", "3", "4",
"2", "7"), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("Observed_Letter",
"Speed")), index = structure(c(1294769521.1, 1294769521.2, 1294769521.4,
1294769521.8, 1294769522.2, 1294769522.2), tzone = "", tclass = c("POSIXct",
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "",
class = c("xts", "zoo"))
此功能将清理每个类别。
clean <- function(x) {
# construct xts object with only Speed and convert it to numeric
out <- xts(as.numeric(x$Speed),index(x))
# add column names
colnames(out) <- paste(x$Observed_Letter[1],"_Per_Second",sep="")
out # return object
}
这是您需要的胆量。请注意需要明确说明
split.default
,因为 xts 对象有一个 split
方法可以按时间拆分。您也不需要 align.time
,但它会将每个周期四舍五入到整秒。否则,您的索引将是每秒索引中的最后一个实际值。# split by Observed_Letter, apply clean() to each list element, and merge results
combA <- do.call(merge, lapply(split.default(A, A$Observed_Letter), clean))
alignA <- align.time(combA,1)
# get the last obs for each 1-second period (for period.apply)
EPalignA <- endpoints(combA, "seconds")
# count the number of non-NA observations by column for each 1-second period
counts <- period.apply(alignA, EPalignA, function(x) colSums(!is.na(x)))
# sum the non-NA observations for each column and 1-second period
values <- period.apply(alignA, EPalignA, colSums, na.rm=TRUE)
# calculate aggregate speed
B <- counts
B$Aggregate_Speed <- rowSums(values)/rowSums(counts)
关于r - 将时间序列中的分类列扩展为多个每秒计数列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6790695/