Esther Esther - 10 days ago 5
R Question

Use two columns as index to calculate a third column

I have one list

>a<-c(4,5,6,7,8)


I have one data.frame

>df<-data.frame(start=c(1,4),end=c(3,5))


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

>df

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.

Answer

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)
Comments