R Basic Syntax
Learning a new language usually starts with outputting a "Hello, World!" program. The R language "Hello, World!" program code is as follows:
## Example (helloworld.R)
myString **Note:** R language assignment uses the left arrow
>
> **Note:** The most common script file extension for R is .R.
>
>
> For example:
>
> hello.R data_analysis.R stock.R
> Although .r can also be used, the community mainstream almost exclusively uses .R.
### Variables
Valid variable names in R language consist of letters, numbers, and dots . or underscores _.
Variable names start with a letter or a dot.
| Variable Name | Valid | Reason |
| --- | --- | --- |
| var_name2. | Valid | Starts with a character, composed of letters, numbers, underscores, and dots |
| var_name% | Invalid | % is an illegal character |
| 2var_name | Invalid | Cannot start with a number |
| .var_name, var.name | Valid | Can start with a dot, but be careful not to follow the dot with a number |
| .2var_name | Invalid | Cannot have a number after the dot |
| _var_name | Invalid | Cannot start with an underscore |
### Variable Assignment
The latest versions of R language assignment can use left arrow :
## Example
# Use equal sign = for assignment
> var.1 =c(0,1,2,3)
>print(var.1)
0 1 2 3
# Use left arrow var.2 print(var.2)
"learn""R"
# Use right arrow -> for assignment
>c(TRUE,1)-> var.3
>print(var.3)
1 1
To view defined variables, you can use the **ls()** function:
## Example
>print(ls())
"var.1""var.2""var.3"
To delete variables, you can use the **rm()** function:
## Example
>rm(var.3)
>print(ls())
"var.1""var.2"
>
In the previous chapter, we learned how to install the R programming environment. Next, we will introduce R language interactive programming and file script programming.
### Interactive Programming
We only need to execute the R command in the command line to enter the interactive programming window:
R
After executing this command, the R language interpreter will be invoked. We can enter code after the > symbol.
!(https
YouTip