【问题标题】:Subsetting and plotting data by TimeStamp按 T​​imeStamp 对数据进行子集化和绘图
【发布时间】:2017-01-10 14:34:53
【问题描述】:

我有一个 data.frame P1 (5000rows x 4cols),当第 1 列中的时间戳落入由向量 TimeStamp (片刻之间)。

例如将第 2、3、4 列中的所有值放入一个新的 data.frame 中,并调用每个数据部分:Condition.1.P1、Condition.2.P1 等。

我想单独标记的原因是因为我有 35 个版本的 P1(P2、P3、P33 等)并且需要能够将它们融合在一起来绘制它们。

dput(TimeStamp)
c(18, 138, 438, 678, 798, 1278, 1578, 1878, 2178)


dput(head(P1))
structure(list(Time = c(0, 5, 100, 200, 500, 1200), SkinTemp = c(27.781, 
27.78, 27.779, 27.779, 27.778, 27.777), HeartRate = c(70, 70, 
70, 70, 70, 70), RespirationRate = c(10, 10, 10, 10, 10, 10)), .Names = c("Time", 
"SkinTemp", "HeartRate", "RespirationRate"), row.names = c(NA, 
6L), class = "data.frame")

【问题讨论】:

    标签: r


    【解决方案1】:

    您想按时间戳范围分隔数据并将其放入列表中吗?这可能是您正在寻找的:

    TimeStamp <- c(18, 138, 438, 678, 798, 1278, 1578, 1878, 2178)
    
    dat <- structure(list(Time = c(0, 5, 100, 200, 500, 1200), SkinTemp =(27.781, 
    27.78, 27.779, 27.779, 27.778, 27.777), HeartRate = c(70, 70, 
    70, 70, 70, 70), RespirationRate = c(10, 10, 10, 10, 10, 10)), .Names = c ("Time", 
    "SkinTemp", "HeartRate", "RespirationRate"), row.names = c(NA, 
    6L), class = "data.frame")
    
    dat$Segment <- cut(dat$Time,c(-Inf,TimeStamp))
    split(dat,dat$Segment)
    

    【讨论】:

    • 感谢您的回答。我认为这给了我正确的格式。既然它是这种格式,我如何只在 TimeStamp 138 和 438 之间绘制一个变量? plot(split(dat,dat$Segment)$(138,438]) 绘制所有变量
    • plot(split(dat,dat$Segment)$(138,438][,1:4]) 应该可以工作。
    【解决方案2】:
    P2 = data.frame(NA, NA, NA, NA) # Create empty data.frame
    for (i in 1:length(ts)){
        P3 = data.frame() # Create empty changing data.frame
        if (i == 1) {ts1 = 0} else {ts1 = ts[i-1]} #First time stamp starts at 0
        ts2 = ts[i]
        P3 = subset(P1, P1$Time > ts1 & P1$Time < ts2)[,c(2,3,4)] #Subset the columns and assign to P3
        if (nrow(P3) == 0){P3 = data.frame(NA, NA, NA)} #If the subset is empty, assign NA
        P3$TimeStamp = paste(ts1,ts2,sep="-") # Append TimeStamp to the P3
        colnames(P3) = colnames(P2) #Make sure column names are same to allow rbind
        P2 = rbind(P2,P3) #Append P3 to P2
    }
    P2 = P2[c(2:nrow(P2)),] #Remove the first row (that has NA)
    colnames(P2) = c("SkinTemp", "HeartRate", "RespirationRate", "TimeStamp") #Provide column names)
    rm(P3); rm(i); rm(ts1); rm(ts2) #Cleanup
    

    【讨论】:

    • 谢谢你。是否可以在我的向量 TimeStamp 中的时间间隔内执行此操作?
    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2019-07-14
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多