English 中文(简体)
以清单中各项目的名称为基础,多次发布数据框架和清单
原标题:Merge a data frame and list based on the names of the items in a list multiple times
  • 时间:2024-04-18 01:22:21
  •  标签:
  • r
  • merge

我期待着将数据框架和清单与多种条件合并起来(“可变”栏和“可变”栏)。

• 合并名单,每个组3个,V1-V5和V1_-V5:

“在此处的影像描述”/</a

以及数据范围:

enter image description here

我希望最后产出能够像上述数据框架一样,但有两个新栏,一栏与清单的“可变”数值,另一栏与“可变”数值。

以“可变”为特征的merge

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( > You list into apointup Media, 然后,paste(Group with Variable or Variable_ toindex into:

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




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签