创建列表时出现意外的符号错误

问题描述 投票:1回答:1

我正在尝试为两个因素的特定水平创建颜色列表。参数如下:

> df.coldata
        Condition  Tank
R235      Control    T6
R236  LowExposure    T6
R239 HighExposure    T6
R241      Control    T8
R242  LowExposure    T8
R245 HighExposure    T8
R247      Control T14_3
R248  LowExposure T14_3
R250 HighExposure T14_3

由于我不想手动填写坦克编号或条件,因此我试图使用分配的变量来创建列表,如下所示:

### Specify colors ####
Tanks <- levels(df.coldata$Tank)
Conditions <- levels(df.coldata$Condition)

ann_colors <- list(
  Condition = c(Conditions[1]="lightskyblue", # This doenst work ... BUGS here!!!
                Conditions[3]="royalblue1",
                Conditions[2]="navyblue"),
  Tank = c(Tanks[1]="gray90",
           Tanks[2]="gray65",
           Tanks[3]="gray40")
)

但是这会告诉我一个错误:

Error: unexpected '=' in:
"ann_colors <- list(
  Condition = c(Conditions[1]="

当我使用以下代码运行代码时:

ann_colors <- list(
  Condition = c(Control="lightskyblue",
                LowExposure="royalblue1",
                HighExposure="navyblue"),
  Tank = c(T14_3="gray90",
           T6="gray65",
           T8="gray40")
)

它就像是一种魅力。我究竟做错了什么?我想念什么吗?

r list unexpected-token
1个回答
3
投票

改为使用setNames

ann_colors <- list(
  Condition = setNames(c("lightskyblue", "royalblue1", "navyblue"), Conditions),
  Tank = setNames(c("gray90", "gray65", "gray40"), Tanks)
  )

您的代码错误的原因是我们试图在c()中为条件/坦克分配一个新值。下面将起作用,并将Conditions的第一个值替换为"lightskyblue",但这是我们想要的not

Conditions[1] = "lightskyblue"
Conditions
# [1] "lightskyblue" "HighExposure" "LowExposure" 

并将其包装在c()中会引发错误:

c(Conditions[1] = "lightskyblue")
# Error: unexpected '=' in "c(Conditions[1] ="
© www.soinside.com 2019 - 2024. All rights reserved.