Esther Esther - 5 months ago 38
R Question

Use two columns as index to calculate a third column

I have one list


I have one data.frame


I want to create a third column in this df based on the start-end


start end
1 1 3 mean(a[1:3])
2 4 5 mean(a[4:5])

of course mean(a[df$start:df$end]) does not work.
I have solved this in a long manner by creating a new data.frame, but I am wondering if is there a short way to do.


We can use mapply to get the seq of corresponding elements of 'start' and 'end' column, subset the 'a' based on that index, get the mean and assign the output to create the new column ('Mean') in 'df'

df$Mean <- mapply(function(x,y) mean(a[seq(x,y)]), df$start, df$end)