The question hast 2 parts.
-
Which is the data structure in R that allows to store the paired data:
0:0 0.5:10 1:20(Python dictionary
{[0]:0, [0.5]:10, [1]:20})and how to initiate it with one liner? i.e. to couple
seq(0,1,by=0.5)
withseq(0,10,by=5)in this data structure -
Assume I added
0.25to the list, then I want the weighted average of the neighbor nodes to appear (automatically) in the data set, i.e. the element0.25:5and the paired set would be0:0 0.25:5 0.5:10 1:20If I add the element
0.3, then it must be paired with5+(10-5)*(0.3-0.25)/(0.5-0.25)=6and element0.3:6to be added.
How I can create the class with S4 or Reference Class class model where I could put this functionality?
A two-column data.frame seems appropriate:
Then, what you are trying to do is a linear-interpolation, which you can achieve using the
approxfunction. For example:If you want to add that result to the data.frame, you can do something like:
which is a bit expensive, especially if you plan to add points one at a time. You could instead add all your points at once since the result is independent of the order in which you add them: