创建列表时出现意外符号错误
Unexpected sign error when creating a list
我正在尝试为两个因素的特定水平创建一个颜色列表。参数如下:
> 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
因为我不想手动填写 Tank 编号或条件,所以我尝试使用分配的变量创建一个列表,如下所示:
### 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")
)
它就像一个魅力。我究竟做错了什么?我错过了什么吗?
改用setNames:
ann_colors <- list(
Condition = setNames(c("lightskyblue", "royalblue1", "navyblue"), Conditions),
Tank = setNames(c("gray90", "gray65", "gray40"), Tanks)
)
您的代码出错的原因是我们正试图在 c()
内为 Conditions/Tanks 分配一个新值。下面会起作用,并将 Conditions 的第一个值替换为 "lightskyblue"
,但这 不是 我们想要的:
Conditions[1] = "lightskyblue"
Conditions
# [1] "lightskyblue" "HighExposure" "LowExposure"
并将其包装在 c()
中会引发错误:
c(Conditions[1] = "lightskyblue")
# Error: unexpected '=' in "c(Conditions[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
因为我不想手动填写 Tank 编号或条件,所以我尝试使用分配的变量创建一个列表,如下所示:
### 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")
)
它就像一个魅力。我究竟做错了什么?我错过了什么吗?
改用setNames:
ann_colors <- list(
Condition = setNames(c("lightskyblue", "royalblue1", "navyblue"), Conditions),
Tank = setNames(c("gray90", "gray65", "gray40"), Tanks)
)
您的代码出错的原因是我们正试图在 c()
内为 Conditions/Tanks 分配一个新值。下面会起作用,并将 Conditions 的第一个值替换为 "lightskyblue"
,但这 不是 我们想要的:
Conditions[1] = "lightskyblue"
Conditions
# [1] "lightskyblue" "HighExposure" "LowExposure"
并将其包装在 c()
中会引发错误:
c(Conditions[1] = "lightskyblue")
# Error: unexpected '=' in "c(Conditions[1] ="