简体   繁体   English

在数据框中标记所有给定值的值

[英]Mark all values given value in dataframe

I have a dataframe with time data, with which I'd like to mark certain rows as belonging to a period of interest. 我有一个带有时间数据的数据框,我想用它来标记某些行属于某个时间段。 For example: 例如:

time <- c(1,2,3,5,7,9,23,24,28,43,45)
action <- c("clap","blink","stare","stare","clap","stare",
"clap","blink","stare","clap","stare")

I'd like to find what happens within, say, 5 second of each clap, hopefully by creating a new column which would have an index marker for each new sequence of clap and post-clap. 我想找到每个拍子5秒内发生的情况,希望通过创建一个新列来为每个新的拍子和拍手序列提供索引标记。

I'm quite a novice with R so there might be a way of phrasing this that would help with searching for an answer, so my apologies if that is the case. 我是R的新手,因此可能有一种措辞措辞可以帮助寻找答案,因此我很抱歉。

I believe this does what you want. 我相信这是您想要的。 Note that I have added a row to your data.frame , since you mention you want an identifier to see if something happens within 5 seconds from a clap. 请注意,我已经在data.frame添加了一行,因为您提到要让标识符查看拍手后5秒钟内是否发生了什么。

# Your data
time <- c(1,2,3,5,7,9,23,24,28,43,45)
action <- c("clap","blink","stare","stare","clap","stare",
            "clap","blink","stare","clap","stare")
df = data.frame(time=time,action=action)

# Added a row that does not occur within 5 seconds of a clap for illustration purposes.
df = rbind(df,data.frame(time=100,action='stare'))

# Add the group number, based on the claps.
df = df %>% mutate(group=cumsum(action=='clap'))

# Check if action is within 5 seconds of a clap.
df = df %>% 
  group_by(group) %>% 
  mutate(within_5_seconds = ifelse(time-time[1]<=5,1,0)) %>%
  as.data.table

Output: 输出:

    time action group within_5_seconds
 1:    1   clap     1                1
 2:    2  blink     1                1
 3:    3  stare     1                1
 4:    5  stare     1                1
 5:    7   clap     2                1
 6:    9  stare     2                1
 7:   23   clap     3                1
 8:   24  blink     3                1
 9:   28  stare     3                1
10:   43   clap     4                1
11:   45  stare     4                1
12:  100  stare     4                0

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

相关问题 替换数据框中的值,直到匹配给定值 - Replace values in a dataframe, until it matches a given value 如何用另一个数据框中的值替换一个数据框中的所有列中的值 - How to replace values in all columns of a dataframe by a value in another dataframe 如何在r中将数据帧中的-1的所有实例标记为NA - How to mark all instances of -1 in a dataframe as NA in r 在R中查找给定数字的2个幂值组合值的所有总和 - Finding all sum of 2 power value combination values of a given number in R 在R中查找给定数字的2个幂值组合值的所有总和 - Finding all sum of 2 power value combination values of a given number in R 如何在R Studio中将所有整数值标记为“ -1”? - How to mark all integer values to “-1” in R Studio? 如何将数据帧中的所有值作为maxLik中的起始值循环 - How to loop all values in a dataframe as the start value in maxLik 子集数据帧,以使每一行中的所有值都小于某个特定值 - Subset dataframe such that all values in each row are less than a certain value 根据条件在迭代中将一个值与数据框中的所有值进行匹配 - Matching one value against all values in a dataframe, in iteration based on criteria 如何用 X 列中的相应值替换数据框中的所有值? - How to replace all values in a dataframe with their respective value in column X?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM