[R] 將值賦予給文字以及全域變數宣告

有時候會將變數或值指定給文字(變數名稱),
讓文字變成新的變數,
就可以用assign
例如 assign('Im_Variable', 'Im Value')
Im_Variable就成為變數,賦予值 'Im Value'。

而有些時候需要宣告全域變數,
那就要增加envir = .GlobalEnv

另一題外話,
一般宣告變數是用 <-
全域變數則是用<<-
下方有小範例。

Sometimes I want to assign value to a variable name (string),
so that the string will become a variable.
In this case,
you can use assign,
All you have to do is to give the function a string,
which is the name you want to give your variable,
and give the function the value you want to assign.
Then the string you set will become the variable,
and it will be assigned the value you set.
When you want to set the variable as a global variable,
just use envir = .GlobalEnv.
The example is as follows:


沒有留言:

張貼留言