根据条件返回特定值:不正确的尺寸数

时间:2019-02-21 02:18:22

标签: r if-statement

    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")

上面是三个数据框A,B和C。下面是一个数据框(D),它与数据框A相同,但是有一列带有我要解决的答案。因此,在运行该过程之后,数据框A应该在数据框D中添加第4列。

如果数据框A的第3列是B,则将数据框B用于下一步。如果数据框A的第3列是C,则将数据框C用于下一步。选择正确的数据框后,查看数据框A的第2列。匹配“值”,并突出显示数据框C中的ROW(因为C是我们在此示例中将使用的数据框)。然后回到数据框A,查看第1列。匹配“值”并突出显示“列”。找到相交的值,并将其放在数据框D的第4列中。冲洗并重复。

我找到了一个运行ifelse语句的解决方案,但出现错误“表_1 [DF2,DF1]中的错误:维数不正确”。我从另一篇文章中读到,您应该将数据框放入矩阵中?

    Table_1 <-
      ifelse(A$Column_3 == "C", C, B)

    DF1 <- 
      ifelse(A$Column_2 =="one", 1,
             ifelse(A$Column_2 =="two", 2,
                    ifelse(A$Column_2 =="three", 3,
                           ifelse(A$Column_2 =="four", 4,
                                  ifelse(A$Column_2 =="five", 5,
                                         ifelse(A$Column_2 =="six", 6,
                                                ifelse(A$Column_2 =="seven", 7,
                                                       ifelse(A$Column_2 =="eight", 8,
                                                                      ifelse(A$Column_2 =="nine", 9,
                                                                             ifelse(A$Column_2 =="ten", 10, ""))))))))))

    DF2 <- 
      ifelse(A$Column_1 == "X.1", 1 + 1,
             ifelse(A$Column_1 == "X.2", 2 + 1,
                    ifelse(A$Column_1 == "X.3", 3 + 1,
                           ifelse(A$Column_1 == "X.4", 4 + 1, ""))))

    A$Column_4 <- Table_1[DF2, DF1]


    D <- structure(list(Column_1 = c("X.1", "X.2", "X.3", "X.4", "X.5", "X.6", "X.7", "X.8", "X.9", "X.10"), 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"), Column_4 = c(50, 61, 72, 34, 5, 85, 66, 73, 51, 1)), row.names = c(NA, 
    -10L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

首先,如果您打算将许多ifelse语句串在一起,那么使用case_when会更容易和更简单。但是,对于这个问题,我认为如果将BC中的数据进行纠缠(将它们设为长数据,然后与A合并)会更简单。

使用dplyrtidyr可以很容易地做到这一点。首先,我们将修改BC

library(tidyr)
library(dplyr)

B_mod <- B %>% 
  gather(-Column_3, key = "identifier", value = "Value") %>% 
  mutate(data = "B")

C_mod <- C %>% 
  gather(-Column_3, key = "identifier", value = "Value") %>% 
  mutate(data = "C")

现在,我们将把这些修改后的数据帧组合成一个单一的长数据帧。

full_mod <- bind_rows(B_mod, C_mod) %>% 
  mutate_if(is.factor, as.character)

最后,我们将A的列与带有full_mod的新创建的left_join数据框进行匹配。在by自变量中,A中的列在左侧,而在full_mod中其对应的列在右侧。

A %>% 
  mutate_if(is.factor, as.character) %>% 
  left_join(full_mod,
            by = c("Column_1" = "identifier",
                   "Column_2" = "Column_3",
                   "Column_3" = "data"))

哪个给了我们

   Column_1 Column_2 Column_3 Value
1       X.1      one        C    50
2       X.2      two        C    61
3       X.3    three        C    72
4       X.4     four        B    34
5       X.1     five        B     5
6       X.4      six        C    85
7       X.2    seven        C    66
8       X.3     four        C    73
9       X.1      two        C    51
10      X.1      one        B     1

编辑:

我添加了mutate_if,以确保将因素转换为字符以改善left_join