R:帮助编写复杂的 if-statements/for-loops 来提取和制表大象的行为

R: Help writing complex if-statements/for-loops to extract and tabulate elephant behavior

我目前正在研究 post-社会雄性大象的冲突行为。我正在尝试大幅加快某些数据的提取和处理速度。我的数据采用以下形式:

Time = c(100, 120, 140, 440, 520, 650, 750) #time is in seconds
Individual1 = c("Luke", "Luke", "Tyler", "Tyler", "Tyler", "Tyler", "Luke")
Action = c("Over shoulder", "Displacement", "Trunk", "Trunk to mouth", "Tail swing", "Ears held out", "Trunk")
Individual2 = c("Tyler", "Tyler", "own mouth", "Luke", "Tyler", "Tyler", "own mouth")

df = data.frame(Time, Individual1, Action, Individual2)

我有兴趣在冲突后 10 分钟内 window 将侵略受害者的行为数据制成表格。在这种情况下,攻击性行为是 "Displacement",即 Luke-Displacement-Tyler。

我想做以下一组操作: 从两个人之间发生位移的时间开始(在本例中为120s),我想向前数600秒(10分钟)并列出所有Individual2 在位移时(在本例中为 Tyler)执行的动作,直到 10 分钟 window 结束,以及这些动作针对的对象(无论是在个人 2 列)。大多数事件由数百个个体之间的互动和许多位移组成。

这是 "apply" 相关职能的工作吗?包含 "table" 调用和一些条件子集命令的 for 循环?

更新:

感谢 Maiasaura 下面的指导,我已经成功地塑造了一系列命令和函数来实现我需要的数据帧列表。但是,我在最后一部分遇到了麻烦,它结合了 mutate() 和 ifelse() 语句。我的最终目标是查看下面函数生成的 post 冲突交互,并确定数据框的 "Action" 列是否包含下面的任何从属行为。

affiliation <- c("Trunk to body", "Other body","Head to head", 
"Trunk to  mouth","Rubs", "Gentle sparing", "Trunk to head", "Head to body",
"Trunk temperal", "Ear on face","Tail to body", "Trunk wrap", "Ear on rear",
"greeting")

这是根据 Maiasaura 的代码编辑的函数:

library(dplyr)
library(purrr)

hostility <- c("Displacement", "Head shake", "Open mouth", "Head held up", 
"Trunk throw", "Chase", "Charge", "Head thrust", "Ear Fold", "Stand off",
"Lunge", "Aggressive ear flap", "Ears held out")

pos <- which(event$Action %in% hostility)

grab_data <- function(pos) {
  i2 <- as.character(event[pos, ]$Individual2)
  i1 <- as.character(event[pos, ]$Individual1)
  action <- as.character(event[pos, ]$Action)
  start <- event[pos, ]$Time
  df2 <- event %>% 
  dplyr::filter(Time > start) %>%
  dplyr::filter(Time <= (start + 600)) %>%
  dplyr::filter((Individual1 == i2 & Individual2 == i1) | (Individual1 == i1 &
  Individual2 == i2)) %>%
  mutate(Displaced = i2, OriginalDisplacer = i1, OriginalConflict = start, 
       Aggression = action, PCAff = ifelse(action %in% affiliation), "1","NA")
  df2
}

df2 <- map(pos, grab_data)

然而,当我应用该函数时,我收到以下警告:

错误:缺少参数 "no",没有默认值

这与嵌套在最后一个mutate() 命令中的ifelse 语句有关。我在这里错过了什么?当不满足条件时,是否需要为 ifelse 提供额外的参数?提前感谢任何 R 智慧!

# Install both packages with install.packages() if you don't have them.
library(dplyr)
library(purrr)

hostility <- c("Displacement") # add other actions as necessary 

# Now we grab the positions where such hostile events occur
# In your example this is only position 2
pos <- which(df$Action %in% hostility)

# This function will take a position,
# then look 600 seconds forward for 
# rows for actions by Individual2

grab_data <- function(pos) {
  i2 <- as.character(df[pos, ]$Individual2)
  start <- df[pos, ]$Time
  df2 <- df %>% 
    dplyr::filter(Time <= (start + 600)) %>%
    dplyr::filter(Individual1 == i2) %>% 
    mutate(actor = i2)
  df2
}

# Now we can "apply" this list of positions to the function. This applies the
# argument on the left (the positions) to the function and returns the output as
# data.frames
map(pos, grab_data)