使用另一个现有列上的条件创建新列

时间:2017-12-09 13:36:13

标签: r dplyr

我有这样的数据

Time    chamber
9       1
10      2
11      3
12      4
13      5
14      6
15      7
16      8
17      9
18      10
19      11
20      12
21      1
22      2
23      3
24      4

我想使用另一个现有色谱柱(腔室)上的条件创建一个新色谱柱。

看起来应该是这样的

Time    chamber treatment
9       1       c2t2
10      2       c2t2
11      3       c0t0r
12      4       c2t2r
13      5       c2t2r
14      6       c0t0
15      7       c0t0r
16      8       c0t0r
17      9       c2t2
18      10      c2t2r
19      11      c0t0
20      12      c0t0
21      1       c2t2
22      2       c2t2
23      3       c0t0r
24      4       c2t2r
  • 对于1,2,9号房间:治疗是c2t2
  • 对于第3,7,8室:治疗为c0t0r
  • 对于4,5,10室:治疗为c2t2r
  • 对于第6,11,12室:治疗为c0t0

我也制作了查找表,但我不知道如何使用它:

lookup_table <- data.frame(row.names = c("1", "2", "3","4", "5", "6","7", "8", "9","10", "11", "12"),
                           new_col = c("C2T2", "C2T2", "C0T0R","C2T2R", "C2T2R", "C0T0","C0T0R", "C0T0R", "C2T2","C2T2R", "C0T0", "C0T0"), 
                           stringsAsFactors = FALSE)    

2 个答案:

答案 0 :(得分:2)

假设&#34; dt&#34;是您的dataframe名称,然后您可以将dplyrcase_when

一起使用
library(tidyverse)
dt %>% 
  mutate(newcol = case_when(dt$chamber %in% c(1, 2, 9) ~ "c2t2",
                            dt$chamber %in% c(3, 7, 8) ~ "c0t0r",
                            dt$chamber %in% c(4, 5, 10) ~ "c2t2r",
                            dt$chamber %in% c(6, 11, 12) ~ "c0t0"))

<强>输出

   Time chamber newcol
1     9       1   c2t2
2    10       2   c2t2
3    11       3  c0t0r
4    12       4  c2t2r
5    13       5  c2t2r
6    14       6   c0t0
7    15       7  c0t0r
8    16       8  c0t0r
9    17       9   c2t2
10   18      10  c2t2r
11   19      11   c0t0
12   20      12   c0t0
13   21       1   c2t2
14   22       2   c2t2
15   23       3  c0t0r
16   24       4  c2t2r
> 

答案 1 :(得分:1)

您可以将dflookup_table合并。根据我的经验,如果要组合不同的data.frames,merge()是我喜欢使用的命令。请注意,有许多不同的方法和专门的包可以用于同一目的!

您需要指定用作&#39;匹配列的列?并且您希望将所有记录保存在df

merge(df, lookup_table, all.x = TRUE, by.x = "chamber", by.y = "row.names")

数据:

df <- structure(list(Time = 9:24, chamber = c(1L, 2L, 3L, 4L, 5L, 6L, 
                     7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L)), 
                    .Names = c("Time", "chamber"), class = "data.frame", 
                    row.names = c(NA, -16L))
lookup_table <- structure(list(new_col = c("C2T2", "C2T2", "C0T0R", "C2T2R", 
                                          "C2T2R", "C0T0", "C0T0R", "C0T0R", 
                                          "C2T2", "C2T2R", "C0T0", "C0T0")), 
                                .Names = "new_col", 
                                row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "data.frame")