Vik*_*k G 2 r vector variable-assignment
我有一个名为 school_name 的变量
我正在创建一个向量来定义稍后将在 ggplot2 中使用的颜色。
colors <- c("School1" = "yellow", "School2" = "red", ______ = "Orange")
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我使用变量 school_name 来实现某些逻辑,希望将其添加为向量的第三个元素。该值在我的 for 循环中发生变化,并且不能进行硬编码。
我已经尝试过以下方法,但它不起作用。
colors <- c("School1" = "yellow", "School2" = "red", get("school_name") = "Orange")
Run Code Online (Sandbox Code Playgroud)
请有人帮我解决这个问题
您可以使用structure
:
school_name = "coolSchool"
colors <- structure(c("yellow", "red", "orange"), .Names = c("School1","School2", school_name))
Run Code Online (Sandbox Code Playgroud)