我有两列。一个是一系列数据的True/False。整个数据集还有一个时间步长列。我想写的代码,可以读取,当布尔列更改为真,时间是从时间戳列计算,直到布尔值更改回假。然后对整个序列重复这个过程,然后把时间放在一个数据帧里,做成直方图。不好意思,我真的不知道从哪里开始。注意,运行列是作为字符列出的——也许我需要将其转换为布尔值才能工作?
running <- c("t","t","f","f","t","f","t","t")
time <- c("2022-01-01 00:00:10", "2022-01-01 00:00:20","2022-01-01 00:00:30","2022-01-01 00:00:40","2022-01-01 00:00:50","2022-01-01 00:01:00","2022-01-01 00:01:10","2022-01-01 00:01:20")
dataset <- data.frame(time, running)
datafinal <- data.frame()
for (i in dataset){
if running == f,
result <- sum(i:n)
datafinal <- c(datafinal, result)
}
将running
列转换为布尔值并使用for循环是一种方法。此外,您还可以在数据框架中进行操作。你已经有一个了!这是一个解决方案工作与tidyverse
库和一些日期操作感谢lubridate
库。我鼓励您学习使用这些库来解决这类问题。
data.table
库中的rleid()
函数每次目标列running
中的值发生变化时增加+1。
running <- c("t","t","f","f","t","f","t","t")
time <- c("2022-01-01 00:00:10", "2022-01-01 00:00:20","2022-01-01 00:00:30","2022-01-01 00:00:40","2022-01-01 00:00:50","2022-01-01 00:01:00","2022-01-01 00:01:10","2022-01-01 00:01:20")
dataset <- data.frame(time, running)
# times to date time object
dataset$time = lubridate::ymd_hms(dataset$time,tz="UTC")
library(tidyverse)
solution = dataset %>%
mutate(Grp=data.table::rleid(running)) %>% # rows in the same state before change get same value
group_by(Grp) %>% # rows in the same state are grouped together
slice(1) %>% # keep first row
ungroup %>% # you don't need grouping anymore
mutate(timeLength = difftime(time, lag(time), units="secs"))
# calculate the differences between a row and previous one (lag(n=1))
输出:
# A tibble: 5 x 4
time running Grp timeLength
<dttm> <chr> <int> <drtn>
1 2022-01-01 00:00:10 t 1 NA secs
2 2022-01-01 00:00:30 f 2 20 secs
3 2022-01-01 00:00:50 t 3 20 secs
4 2022-01-01 00:01:00 f 4 10 secs
5 2022-01-01 00:01:10 t 5 10 secs
如果您想要删除第一行NA,只需添加到管道%>% filter(!is.na(timeLength))
。
更新,添加如何使用for循环和嵌套if-else来实现。但请注意,代码较长,更难以跟踪。
dataset$time = lubridate::ymd_hms(dataset$time,tz="UTC")
# empty array for tracking changes in rows
current = c()
# datafinal empty dataframe
datafinal = data.frame()
# better working with the rows index
for (i in seq(nrow(dataset))){
# extract current vale of running
current = c(current,dataset[i,]$running)
if (i>1){ # we can't operate with first row, right?
if (current[i] == current[i-1]){
next # pass iteration if they keep in same state (true or false)
}
else { # different state? let's operate
result = difftime(dataset[i,]$time, previous_time, units="secs")
}
# (note: if 'next' jump in if-loop this part doesn't jump)
# create the outcome row for iteration
new_row = cbind(dataset[i,],result)
# add row to final dataframe
datafinal = rbind(datafinal,new_row)
}
# keep first time of state when it changes or we initiate the loop
previous_time = dataset[i,]$time
}