根据来自不同列的位置计算数据框中的平均值

Vin*_*woo 5 r mean dataframe

我有一个数据框设置如下:

N1 <- c(1,2,4,3,2,3,4,5,4,3,4,5,4,5,6,8,9)
Start <- c("","Start","","","","","","","Start","","","","Start","","","","")
Stop <- c("","","","","Stop","","","","","","Stop","","","","Stop","","")
Run Code Online (Sandbox Code Playgroud)

N1是我感兴趣的数据.我想根据接下来两列中的"开始"和"停止"位置计算一串数字的平均值.

"开始"和"停止"定义的字符串如下所示:

2,4,3,2 
4,3,4
4,5,6
Run Code Online (Sandbox Code Playgroud)

所以我的最终结果应该是3个意思:

    2.75,3.6,5
Run Code Online (Sandbox Code Playgroud)

Cat*_*ath 5

你可以试试:

mapply(function(start, stop){
          mean(N1[start:stop])
       }, 
       start=which(Start!=""), 
       stop=which(Stop!=""))

#[1] 2.750000 3.666667 5.000000
Run Code Online (Sandbox Code Playgroud)