Learn a new language and take the lead in learning to output Hello World. Let's start with the study.
First open the Rstudio IDE and then enter it on the left:
1 " Hello World " 2 > Print (MYSTR)
, when we finish on the left side, we can see the variable we just had.
Here, the assignment statements in the R language are: variable <-value , different language =, but can also use = assignment, but not recommended. Output statement is print (value)
We can use the class () method to view its type.
Also, in the R language , #是注释, unlike//or/**/in other languages, if we create a method A, then write a comment for the method:
in the R language, the R language, in contrast to other programming languages (such as C and Java), is not declared as certain data types. The variable is assigned to the r object, and the data type of the R object is converted to the data type of the variable. There are many types of R objects. Common R objects are
- Vector
- List
- Matrix
- Array
- Factor
- Data frame
The simplest of these objects is a vector object, and the Vector object has an atomic vector of six data types, also called a six class vector. Other R objects are built on top of an atom vector. The six class vector type is shown in the following table
| Data Type |
Example |
Validation Code |
Output Results |
| Logic |
TRUE, FALSE |
v <- TRUE ; print(class(v)); |
[1] "logical" |
| numeric value |
1, 2.3, 92 |
v <- 23.5 ; print(class(v)); |
[1] "numeric" |
| Integer |
1L, 22L, 0L |
v <- 2L ; print(class(v)); |
[1] "integer" |
| Plural |
3 + 2i |
v <- 2+5i ; print(class(v)); |
[1] "complex" |
| Character |
' A ', ' abc ', ' TRUE ', ' 12.2 ' |
v <- "TRUE" ; print(class(v)); |
[1] "character" |
| Native |
"Hello"The stored values are:48 65 6c 6c 6f |
v <- charToRaw("Hello"); print(class(v)); |
[1] "raw" |
In R programming, very basic data types are r objects called vectors that hold elements of different classes, as shown above. Note that in the R language, the number of types is not limited to the six types listed above, as well as vectors, lists, and so on, so we'll learn more about these types in detail next.
1. Vectors:
--can only contain objects of the same type
--Creating vectors
Create a vector, one in three different ways. 1. Use vector () 2. Direct Assignment 3.c (). Such as:
In R, the system also converts different data types in the vector to the same type.
For example, when the vector X3 is assigned, it is logical true, the value 1, the character ABC, and the system is automatically converted to a character representation at the time of output.
When the vector x4 is assigned, it is the character a,b,c and we can all convert to the character type. However, the default value is generated.
2. Matrices (matrix)
--Vector + dimension attribute (integer vector: nrow,ncol)
--Create a matrix: 1. Use matrix () 2. First, a vector is added to the dimension attribute.
Here, you can see a matrix of 3 rows, 2 columns, and a default NA
We then create a matrix and assign a value of 1 to 6, where we can see that the first column is filled first and then the second column ....
Where the matrix has a dim (), it can be shown that the matrix has several rows of several columns. For example, X1 has three rows and two columns.
The properties of the matrix can be displayed by attributes ()
Here, as previously mentioned, the Matrix = vector + dimension information. So we can create a vector and then add dimension information to it to create a matrix, such as
Learn how to stitch two matrices into a matrix.
Matrix Y,y1:
Splicing two matrices by line:Rbind ()
Splicing two matrices by column:Cbind ()
3. Arrays (Array)
--Similar to a matrix, except that the dimension can be greater than or equal to 2
--Create a matrix: Use array ()
Create an array of 1 to 24, Dim = C (4,6) is specified that he has four rows, six columns. The dimension equals 2, or it can be said to be a matrix. For example:
Create an array of 1 to 24, Dim = C (2,3,4) is the specified dimension is 2,3,4. His dimension is greater than 2 and is a typical array. For example:
4. Lists (list)
--can contain different types of objects
--Create:list ()
Create a list with elements of character A, number 12, Integer 2, complex 2+3i, and logical true.
Each element goes to an individual name.
Create a list of two vectors for each element.
5. Factor (factor)
--is an R object created using a vector.
--Create factor ()
Create a factor that identifies a group of men and women, levels default
Levels himself made
View the number of elements:table ()
Levels is the attribute of the factor, we take a look at the attribute. Unclass ()
6. Data frame
--is a tabular data object. Each Column object can be different.
--Create:data.frame ()
Output:
R language Basic Syntax