1

The question hast 2 parts.

  1. 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) with seq(0,10,by=5) in this data structure

  2. Assume I added 0.25 to the list, then I want the weighted average of the neighbor nodes to appear (automatically) in the data set, i.e. the element 0.25:5 and the paired set would be

    0:0
    0.25:5  
    0.5:10  
    1:20  
    

    If I add the element 0.3, then it must be paired with 5+(10-5)*(0.3-0.25)/(0.5-0.25)=6 and element 0.3:6 to be added.

How I can create the class with S4 or Reference Class class model where I could put this functionality?

Max Li
  • 5,069
  • 3
  • 23
  • 35

2 Answers2

1

Not really sure what you are getting at but maybe the package hash may have what you want

library(hash)
h<-hash(keys=seq(0,1,by=0.5),values=seq(0,10,by=5))
h[['0.25']]<-2.5

Probably deals with the first part of your question. http://cran.r-project.org/web/packages/hash/hash.pdf may allude to help on the second.

a similar construct with lists

lst<-list()
lst<-seq(0,10,5)
names(lst)<-seq(0,1,0.5)
> lst['0.5']
0.5 
  5 
lst['0.25']<-2.5

for your second part you could construct a simple function to update you hash/list with a new value.

shhhhimhuntingrabbits
  • 7,397
  • 2
  • 23
  • 23
  • I would classify it as a problem or at least as an inconvenience that I have to convert the type in order to access the key, I'm looking for exactly lst[0.5] and not lst['0.5']. "you could construct a simple function to update you hash/list with a new value" - the very question is how to implement it in R – Max Li Jul 05 '12 at 21:33
  • I'd like to have a class which has a paired data structure (10% of the wish) and I'd like to implement a method (90% of the wish) "add" (or whatever you call it) in this class that can do this: command "class_instance$add(0.3)" makes this possible: command "class_instance[0.3]" results in 6. What class_instance[1.7] would be, is not of importance (let's throw the error if we want to add a member which is more than maximal existing key, for example) – Max Li Jul 05 '12 at 22:50
  • actually I even reduced the 2nd part of the question to this http://stackoverflow.com/questions/11353135/r-change-the-fields-slots-values-of-the-class-assigning-a-value-to-some-oth – Max Li Jul 05 '12 at 22:53
  • +1 and thanks for the discussion and acceptable solution for the 1-st point – Max Li Jul 09 '12 at 16:27
0

A two-column data.frame seems appropriate:

xy <- data.frame(x = seq(0, 1, by = 0.5), y = seq(0, 20, by = 10))
xy
#     x  y
# 1 0.0  0
# 2 0.5 10
# 3 1.0 20

Then, what you are trying to do is a linear-interpolation, which you can achieve using the approx function. For example:

approx(xy$x, xy$y, xout = 0.3)
# $x
# [1] 0.3
# 
# $y
# [1] 6

If you want to add that result to the data.frame, you can do something like:

xy <- as.data.frame(approx(xy$x, xy$y, xout = sort(c(xy$x, 0.3))))
xy
#     x  y
# 1 0.0  0
# 2 0.3  6
# 3 0.5 10
# 4 1.0 20

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:

add.points <- c(0.25, 0.3)
xy <- as.data.frame(approx(xy$x, xy$y, xout = sort(c(xy$x, add.points))))
xy
#      x  y
# 1 0.00  0
# 2 0.25  5
# 3 0.30  6
# 4 0.50 10
# 5 1.00 20
flodel
  • 87,577
  • 21
  • 185
  • 223