-3

I have 1 data frame, I want to to go from A to F by following sequence of A-B-C-D-E-F. How to proceed using R.

> m
 V1 V2 V3
1  A  B 0.1
2  B  C 0.2
3  C  D 0.3
4  D  E 0.2
5  E  F 0.5
  • 2
    What do you mean? Process the df by rows matching specific numbers? What is the intended outcome and what have you tried so far? – nya Aug 10 '16 at 11:15
  • It is not clear what you wanted. May be check the `library(igraph)` – akrun Aug 10 '16 at 11:17
  • Ok, Forget about that, I will simplify the case, I want to get Value of F. which I dont have so I will check with its dependent variable which is previous one E, If that also dont have I want to go again 1 step back. I have value in A. each step have some multiplication factor. How can I get value of F from A by following sequence (F-E-D-C-B-A) – Ajay Jadhav Aug 10 '16 at 11:22
  • 1
    Really unclear. What is the input data to your procedure? What data have you got to start with? – renato vitolo Aug 10 '16 at 11:28
  • A*0.1 = B, B*0.2 = C, V1* V3 = V2, I want value of F from A by traversing through the sequence A-B-C-D-E-F. As there is no direct relationship between A to F. – Ajay Jadhav Aug 10 '16 at 11:33
  • How to make sense of A* 0.1 = B , could you elaborate – Silence Dogood Aug 10 '16 at 12:57

1 Answers1

2

The way I understand your comments, the relationship between A and F is the product of m$V3 between their rows.

af <- function(from, to){
    x <- which(m$V1 == from)
    y <- which(m$V2 == to)
    return(prod(m$V3[x:y]))
}

 af("A", "F")
[1] 6e-04

Then, F = A * 0.0006.

To generalize to any sequence and any row order in the table, we first define the sequence.

sq <- c("A", "B", "C", "D", "E", "F") # or LETTERS[1:6] in this case

Within the function, we select the respective rows as those, where both columns V1 and V2 contain sequence conditions that match the specification.

af2 <- function(from, to){
    cond <- sq[which(sq == from):which(sq == to)]
    x <- m$V1 %in% cond & m$V2 %in% cond
    return(prod(m$V3[x]))
}

Test

Using the original matrix, both functions provide identical results.

af("B","E")
[1] 0.012
af2("B","E")
[1] 0.012

When we randomise row order, only the second function returns the correct result.

set.seed(123456)
m <- m[sample(1:5),]
m
  V1 V2  V3
4  D  E 0.2
5  E  F 0.5
2  B  C 0.2
1  A  B 0.1
3  C  D 0.3

af("B","E")
[1] 0.02
af2("B","E")
[1] 0.012
nya
  • 2,138
  • 15
  • 29
  • Or directly `A*prod(m$V3)` – Robert Aug 10 '16 at 13:08
  • @Robert True, if the desired relationship always asks for the whole df. – nya Aug 10 '16 at 13:09
  • @nya You got my question. I am sorry but i want to add complexity in this. What if this A- F is not in sequence ? example 2nd row is C - D, third row is B - C, in this case this (prod(m$V3[x:y])) wont work. rit ? – Ajay Jadhav Aug 10 '16 at 16:57
  • @AjayJadhav I added the generalization. In the future, provide examples in your questions that represent the problem in full. Thank you (on behalf of the community, I believe). – nya Aug 11 '16 at 06:27