简体   繁体   中英

How to get the slopes of different sets of data in excel, using the offset function?

I have a dataset structured such that the x-values representing year number goes from 1 to 18, but the dataset contains over 500 instances of the years going from 1 to 18 with different y-values for each instance.

x_values    y_values
1             0.10
2             0.20
3             0.25
.             .
.             .
.             .
18            16.7
1             0.13
2             0.18
3             0.22
.             .
.             .
.             .
18            17.1

where this pattern repeats over 500 times.

I would like to calculate the slopes for each of the instances, using the slope function.

In a previous scenario, I wanted to find the average y-value for each instance, and I used the formula:

=AVERAGE(OFFSET($B$2,(ROW()-ROW($C$2))*18,,18,))

Where $B$2 was my reference cell indicating the first y-value in the first instance.

I would like to apply a similar formula to calculate the slopes, but so far I'm getting a #N/A error when I use this formula:

=SLOPE(OFFSET($B$2,(ROW()-ROW($D$2))*18,,18,),$A$2:$A$19)

where the range $A$2:$A$19 represents the 18 x-values.

Any help on why this error occurred and how to fix it would be greatly appreciated.

If you step through this formula with Evaluate Formula, you will see that there are curly brackets round the row numbers - so the Row functions are delivering a single-element array. You can make them into scalars by using Index:

=SLOPE(OFFSET($B$2,INDEX((ROW()-ROW($D$2))*18,1),0,18,1),$A$2:$A$19)

Note 1:

Better to use Index and avoid using volatile functions like Offset:

=SLOPE(INDEX(B:B,(ROW()-ROW($B$2))*18+2):INDEX(B:B,(ROW()-ROW($B$2))*18+19),$A$2:$A$19)

Note 2:

I can't fully explain why you get this behaviour with Slope but Average is OK. It seems that Average can handle an array of arrays like this:

=SUMPRODUCT(AVERAGE(OFFSET(A2,{1,2},0,1,1)))

while Slope can't, but it still doesn't entirely explain what you see when you step through the formula.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM