SoFunction
Updated on 2025-04-10

Summary of R language constant knowledge points

The basic data types of R language include numerical types, logical types (TRUE, FALSE), and text (string). Support missing values, with special plural types.

Constants refer to values ​​written directly in the program.

Numerical constants include integer, single precision, double precision, etc., and generally do not need to be distinguished. The writing methods are as follows: 123, 123.45, -123.45, -0.012, 1.23E2, -1.2E-2, etc. In order to indicate that 123 is an integer, it can be written as 123L.

Character constants are surrounded by two double apostrophes or two single apostrophes, such as "Li Ming" or 'Li Ming'. Character types support Chinese, such as "Li Ming" or "Li Ming". The Chinese encoding in China mainly includes GBK encoding and UTF-8 encoding. Sometimes you will encounter the problem of garbled code caused by encoding errors. R programs under MS Windows are generally encoded with GBK, but RStudio software uses UTF-8 encoding. Strings in R software are generally saved in UTF-8 encoding.

Logical constants are only TRUE and FALSE.

Missing values ​​are expressed in NA. Missing values ​​are often encountered in statistical calculations, indicating that records are lost, cannot be used due to errors, and no data during holidays. In addition to numerical types, logical types and character types can also have missing values, and the blank values ​​of the character type will not be automatically identified as missing values, and they need to be specified by themselves. R supports special Inf value, which is a real value, indicating positive infinity and does not count as missing values.

The writing methods of complex constants are such as 2.2 + 3.5i, 1i, etc.

Knowledge point expansion:

constant

The constants in R are basically divided into four types: logical, numeric, character and factor. Here are the commands to generate factors:

x<-c("beijing","shanghai","beijing","beijing","shanghai")
y<-factor(x)
y

It can also be written as:

y&lt;-factor(c(1,0,1,1,0)) #Here levels are the factor level, indicating which factors are there.c() is a connection function, connecting a single scalar into a vector.y
y==0 #With variable names,First, you canyand0Make a comparison

This is the end of this article about summarizing the knowledge points of R language constants. For more related R language constant content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!