mercredi 20 février 2019

Return value based on specific conditions

    A <- structure(list(Column_1 = structure(c(1L, 2L, 3L, 4L, 1L, 4L, 
    2L, 3L, 1L, 1L), .Label = c("X.1", "X.2", "X.3", "X.4"), class = "factor"), 
        Column_2 = c("one", "two", "three", "four", "five", "six", 
        "seven", "four", "two", "one"), Column_3 = c("C", "C", "C", 
        "B", "B", "C", "C", "C", "C", "B")), row.names = c(NA, -10L
    ), class = "data.frame")

    B <- structure(list(Column_3 = structure(c(5L, 10L, 9L, 3L, 2L, 7L, 
    6L, 1L, 4L, 8L), .Label = c("eight", "five", "four", "nine", 
    "one", "seven", "six", "ten", "three", "two"), class = "factor"), 
        X.1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), X.2 = c(11, 12, 13, 
        14, 15, 16, 17, 18, 19, 20), X.3 = c(21, 22, 23, 24, 25, 
        26, 27, 28, 29, 30), X.4 = c(31, 32, 33, 34, 35, 36, 37, 
        38, 39, 40)), row.names = c(NA, -10L), class = "data.frame")

    C <- structure(list(Column_3 = structure(c(5L, 10L, 9L, 3L, 2L, 7L, 
    6L, 1L, 4L, 8L), .Label = c("eight", "five", "four", "nine", 
    "one", "seven", "six", "ten", "three", "two"), class = "factor"), 
        X.1 = c(50, 51, 52, 53, 54, 55, 56, 57, 58, 59), X.2 = c(60, 
        61, 62, 63, 64, 65, 66, 67, 68, 69), X.3 = c(70, 71, 72, 
        73, 74, 75, 76, 77, 78, 79), X.4 = c(80, 81, 82, 83, 84, 
        85, 86, 87, 88, 89)), row.names = c(NA, -10L), class = "data.frame")

Above are three dataframes A, B, and C. Below is one dataframe which is the same as dataframe A, but has an extra column with the answers I am trying to solve for. So after I run the process, dataframe A should add column 4 from dataframe D.

If dataframe A column 3 is B then use dataframe B for the next step. If dataframe A column 3 is C then use dataframe C for the next step. Once the correct dataframe has been selected, look at dataframe A column 2. Match the "value" and highlight the ROW in dataframe C (since C is the dataframe we would be using for this example). Then go back to dataframe A and look at column 1. Match the "value" and highlight the COLUMN. Find the intersecting value and put it is dataframe D column 4. Rinse and repeat.

I have tried using if, elseif, ifelse, and match functions, but always seem to run into problems on exactly how to do something like this. I haven't found anyone else with this problem on either r or VBA, which is orginally when I had this problem. Any thoughts on how to go about this? If you need more clarification, please don't hesitate to ask.

    D <- structure(list(Column_1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Column_2 = c("one", 
    "two", "three", "four", "five", "six", "seven", "eight", "nine", 
    "ten"), Column_3 = c("C", "C", "C", "B", "B", "C", "C", "C", 
    "C", "B"), Column_4 = c(50, 61, 72, 34, 5, 85, 66, 73, 51, 1)), row.names = c(NA, 
    -10L), class = "data.frame")

Aucun commentaire:

Enregistrer un commentaire