我希望在R中使用多个条件("Variable"和"Variable_"列)将收件箱和列表合并在一起.

想要合并包含3个组的列表,每个组包含V1-V5和V1_-V5_:

$GROUP1
$GROUP1$V1
[1] 0.857138

$GROUP1$V2
[1] 1

$GROUP1$V3
[1] 0.5

$GROUP1$V4
[1] "not limiting"

$GROUP1$V5
[1] 0.1


$Group2
$Group2$V1
[1] 0.65

$Group2$V2
[1] 1

$Group2$V3
[1] 1

$Group2$V4
[1] 0.6

$Group2$V5
[1] 0.25
...

还有一个摇篮:

   Col.A Col.B  Group Variable_ Variable
1      x     x Group1       V1_       V1
2      x     x Group1       V2_       V2
3      x     x Group1       V3_       V3
4      x     x Group1       V4_       V4
5      x     x Group1       V5_       V5
6      x     x Group2       V1_       V1
7      x     x Group2       V2_       V2
8      x     x Group2       V3_       V3
9      x     x Group2       V4_       V4
10     x     x Group2       V5_       V5
11     x     x Group3       V1_       V1
12     x     x Group3       V2_       V2
13     x     x Group3       V3_       V3
14     x     x Group3       V4_       V4
15     x     x Group3       V5_       V5

我希望最终输出看起来像上面的收件箱,但有两个新列,一列包含列表中"Variable"的值,另一列包含"Variable_"的值

要通过一个条件合并的代码通过"Variable"合并

List |> 
  unlist() |> 
  enframe() |> 
  separate_wider_delim(cols = name, names = c("Group", "Variable"), delim = ".") |> 
  right_join(data)

data = structure(list(Col.A = c("x", "x", "x", "x", "x", "x", "x", "x", 
                                "x", "x", "x", "x", "x", "x", "x"), Col.B = c("x", "x", "x", 
                                                                              "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"), 
                      Group = c("Group1", "Group1", "Group1", "Group1", "Group1", 
                                "Group2", "Group2", "Group2", "Group2", "Group2", "Group3", 
                                "Group3", "Group3", "Group3", "Group3"), Variable_ = c("V1_", 
                                                                                      "V2_", "V3_", "V4_", "V5_", "V1_", "V2_", "V3_", "V4_", "V5_", "V1_", 
                                                                                      "V2_", "V3_", "V4_", "V5_"), Variable = c("V1", 
                                                                                                                             "V2", "V3", "V4", "V5", "V1", "V2", "V3", "V4", "V5", "V1", 
                                                                                                                             "V2", "V3", "V4", "V5")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                    -15L))

List = list(GROUP1 = list(V1 = 0.857138, V2 = 1, V3 = 0.5, 
                          V4 = "not limiting", V5 = 0.1), 
            Group2 = list(V1 = 0.65, V2 = 1, V3 = 1, 
                          V4 = 0.6, V5 = 0.25), 
            Group3 = list(V1 = 0.65, V2 = 0.75, V3 = 0.3, 
                          V4 = 1, V5 = 1),
            GROUP1 = list(V1_ = "x", V2_ = "x", V3_ = "x", 
                          V4_ = "x", V5_ = "x"), 
            Group2 = list(V1_ = "x", V2_ = "x", V3_ = "x", 
                          V4_ = "x", V5_ = "x"), 
            Group3 = list(V1_ = "x", V2_ = "x", V3_ = "x", 
                          V4_ = "x", V5_ = "x"))

推荐答案

将您的列表unlist()转换为命名查找载体,然后将paste() GroupVariableVariable_索引到其中:

library(dplyr)
library(stringr)

# `str_to_title()` to match "GROUP1" with "Group1"
lookup <- List |>
  setNames(str_to_title(names(List))) |>
  unlist()

data |>
  mutate(
    Value_ = lookup[paste(Group, Variable_, sep = ".")],
    Value = lookup[paste(Group, Variable, sep = ".")]
  )

结果:

   Col.A Col.B  Group Variable_ Variable Value_        Value
1      x     x Group1       V1_       V1      x     0.857138
2      x     x Group1       V2_       V2      x            1
3      x     x Group1       V3_       V3      x          0.5
4      x     x Group1       V4_       V4      x not limiting
5      x     x Group1       V5_       V5      x          0.1
6      x     x Group2       V1_       V1      x         0.65
7      x     x Group2       V2_       V2      x            1
8      x     x Group2       V3_       V3      x            1
9      x     x Group2       V4_       V4      x          0.6
10     x     x Group2       V5_       V5      x         0.25
11     x     x Group3       V1_       V1      x         0.65
12     x     x Group3       V2_       V2      x         0.75
13     x     x Group3       V3_       V3      x          0.3
14     x     x Group3       V4_       V4      x            1
15     x     x Group3       V5_       V5      x            1

R相关问答推荐

保存shiny 的代码嗅探器:避免$ Symbol问题

如何从其他前面列中减go 特定列的平均值?

R创建一个数据透视表,计算多个组的百分比

基于多列将值链接到NA

par函数中的缩写,比如mgp,mar,mai是如何被破译的?

plotly hover文本/工具提示在shiny 中不起作用

如何在科学记数法中显示因子

使用R中的dist()迭代ID匹配的欧几里德距离

是否有新方法来更改Facet_WRAP(Ggplot2)中条文本的文本 colored颜色 ?

使用geom_iles在一个切片中包含多个值

如何使用FormC使简单算术运算得到的数字是正确的?

如何为混合模型输出绘制不同的线型?

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

如何将EC50值绘制在R中的剂量-react 曲线上?

如何合并不同列表中的数据文件,包括基于名称的部分匹配,而不是一对一等价

在R中使用ggraph包排列和着色圆

对数据帧中的列进行子集设置以通过迭代创建新的数据帧

如何使用ggsurvfit包更改风险表中的标签名称?

通过不完全重叠的多个柱连接

如何在R曲线图弹出窗口中更改r和theta标签