【问题标题】:creating iterative named lists, with variable names including with operators创建迭代命名列表,变量名包括运算符
【发布时间】:2019-10-01 08:22:26
【问题描述】:

已解决

pheno_vector = c("PAX5+PDL1-", "CD3+CD8+PD1-", "CD3+CD8-PD1+")
n = length(pheno_vector)
for (i in 1:(n-1)){
  for (j in (i+1):n){
    pheno1 = pheno_vector[i]
    pheno2 = pheno_vector[j]
    pairs = list(c(pheno1, pheno2))

    colors = c("cyan", "magenta")
    names(colors) = c(pheno1,pheno2)
    print(colors)

生产

               PAX5+PDL1- CD3+CD8+PD1- 
                "cyan"    "magenta" 
               PAX5+PDL1- CD3+CD8-PD1+ 
                "cyan"    "magenta" 
             CD3+CD8+PD1- CD3+CD8-PD1+ 
                "cyan"    "magenta"

我在创建命名列表时遇到问题,其中名称应该能够迭代更改,但应该粘贴以进行可视化绘图。

我正在处理来自 Vectra Imaging 的数据,涉及细胞表型。

由于有多种表型,我想迭代地制作成对的表型以直观地制作图,如https://rdrr.io/github/PerkinElmer/phenoptr/man/spatial_distribution_report.html 中的示例所示

目前我的代码适应我的数据看起来像这样

pheno1 = "PAX5+PDL1-"
pheno2 = "CD3+CD8+PD1-"
pairs = list(c("PAX5+PDL1-", "CD3+CD8+PD1-"))
colors = c('PAX5+PDL1-'="cyan", 'CD3+CD8+PD1-'="magenta")
colors

返回

>  PAX5+PDL1- CD3+CD8+PD1- 
>     "cyan"    "magenta" 

成对和颜色用于绘制图。现在我想迭代地为数据中的更多表型对绘制图。最后,我想对 pheno1 进行 for 循环,并对 pheno2 进行 for 循环,该循环是从包含 .

我尝试的是

pheno1 = "PAX5+PDL1-"
pheno2 = "CD3+CD8+PD1-"
pairs = list(c(pheno1, pheno2))
colors = c(pheno1="cyan", pheno2="magenta")
colors

返回

>     pheno1     pheno2 
>     "cyan"    "magenta" 

我明白为什么我看到的是 pheno1 和 pheno2,而不是“PAX5+PDL1-”和“CD3+CD8+PD1-”。 我尝试了Create a variable name with "paste" in R? 中的一些粘贴和评估方法,但没有成功。

Assign("PAX5+PDL1-", "cyan")

不成功,因为我不知道如何将其放入命名列表。

最后我想得到这个结果

pheno_vector = c("PAX5+PDL1-", "CD3+CD8+PD1-", "CD3+CD8-PD1+")
for (pheno1 in pheno_vector){
 for (pheno2 in pheno_vector){
  if (uniqueness_statement and permutation_statement){
     pairs = list(c(pheno1, pheno2))
     colors = c(pheno1="cyan", pheno2="magenta")
     colors

哪个应该返回

>     PAX5+PDL1-     CD3+CD8+PD1- 
>     "cyan"             "magenta" 
>     PAX5+PDL1-     CD3+CD8-PD1+ 
>     "cyan"              "magenta" 
>     CD3+CD8+PD1-   CD3+CD8-PD1+
>     "cyan"           "magenta" 

所以这些是专门命名的列表,它们需要用于以后的绘图。

有人有解决办法吗?

【问题讨论】:

    标签: r string list assign names


    【解决方案1】:

    您可以在创建 colors-vector 后为其命名:

    library(gtools)
    
    pheno_perms <- permutations(length(pheno_vector), 2, pheno_vector)
    combs <- apply(apply(pheno_perms, 1, sort), 2, paste0, collapse=' ')
    pheno_perms <- pheno_perms[match(unique(combs), combs), ]
    
    for (i in 1:nrow(pheno_perms)){
      pheno1 <- pheno_perms[i, 1]
      pheno2 <- pheno_perms[i, 2]
      pairs = list(c(pheno1, pheno2))
      colors = c("cyan", "magenta")
      names(colors) <- c(pheno1, pheno2)
      print(colors)
    }
    
    

    这会产生:

    CD3+CD8-PD1+ CD3+CD8+PD1- 
          "cyan"    "magenta" 
    CD3+CD8-PD1+   PAX5+PDL1- 
          "cyan"    "magenta" 
    CD3+CD8+PD1-   PAX5+PDL1- 
          "cyan"    "magenta" 
    

    P.S.:我希望我能正确理解您所说的 uniqueness_statementpermutation_statement 的意思!

    【讨论】:

    • 感谢您的回复。似乎我的名称(颜色)的语法错误。现在已修复,请参阅我的编辑。排列和唯一性陈述不是出于重要性,而是想说明为什么输出有 3 个元素。
    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多