R language Data type

Source: Internet
Author: User

When writing any programming language program, you need to use different variables to store a variety of information. A variable is simply a stored value used to preserve the memory location. This means that when a variable is created, it retains some space in memory.

You might like to store data types other than characters, such as: Wide character, integer, float, double float, Boolean, and so on. Based on the variable data type, the operating system allocates memory and determines what can be stored in the memory.

In other programming languages, variables such as C and Java R are not declared as certain data types. The variable assignment R-object and the data type of the R object become the data type of the variable. There are many types of R-objects. Commonly used are:

    • vectors, list, Matrix, Array, factor, Data Frame

These objects are the simplest vector objects and these atom vectors have six data types, also known as six classes of vectors. In addition R-objects are built on atomic vectors.

Data Type Example Validation
Logic TRUE, FALSE
<-Print(class(v))     
It produces the following results:
Digital 12.3, 5, 999
<-23.5Print(class(v))      
It produces the following results:
[1] "Numeric"
integer 2L, 34L, 0L
v <-  2l print ( class (v           
It produces the following results:
[1] "integer" 
plural 3 + 2i
< Span class= "PLN" >v <- 2+ 5i print (class (v             
It produces the following results:
[1] "complex" 
character ' A ', ' good ', ' TRUE ', ' 23.4 '
Original "Hello" will be stored as 6c 6c 6f
<- chartoraw("Hello")print(class(v)    ) 
It produces the following results:

Therefore, the very basic data types in the R language are R-objects, which occupy different classes of element vectors as shown. Please note that the number of classes in the R language is not limited to the six types mentioned above. For example, we can use many atom vectors and create an array whose classes will be arrays.

Vector (one-dimensional array)

When you want to create vectors using multiple elements, you should use the C () function, which means that the elements are combined into a vector.

# Create a vector.apple <-C (' Red ', ' green ', ' yellow ') print (apple) # Get The class of the Vector.print (class Apple)

When the above code executes, it produces the following result:

[1] "Red"    "green"  
List (equivalent to collection Class)

Lists are R-objects, and the list() function can contain several different types of elements, such as vectors, functions, or even another list.

# Create a list.list1 <-list (c (2,5,3), 21.3,sin) # Print the List.print (list1)

When the above code executes, it produces the following result:

[[1]] [1] 2 5 3[[2]][1] 21.3[[3]]function (x)  
Matrix (awesome two-dimensional array)

A matrix is a two-dimensional rectangular data set. It can be created using a vector input to a matrix function.

# Create a matrix. m = Matrix (c (' A ', ' a ', ' B ', ' C ', ' B ', ' a '), Nrow=2,ncol=3,byrow = TRUE)print (m)

When the above code executes, it produces the following result:

     [, 1] [, 2] [, 3] [1,] "A"  "a" "  B" [2,] "C"  "B"  "a"  
Array

Although the matrix is limited to two dimensions, the array can be any number of dimension sizes. The array function uses it to create the desired number of attributes for the dimension-dim. In the following example, we create an array of two elements, which is a 3x3 matrix.

# Create an array.a <-array (c (' green ', ' yellow '), Dim=c (3,3,2)) print (a)

When the above code executes, it produces the following result:

,, 1     [, 1]     [, 2]     [, 3]    [1,] "green"  "yellow" "Green" [2,] "yellow" "green"  "Yellow" [3,] "green" c6/> "Yellow" "green", 2     [, 1]     [, 2]     [, 3]    [1,] "yellow" "green"  "Yellow" [2,] "green"  " Yellow "Green" [3,] "yellow" "green"  "yellow"   
Factor

A factor is an R object created using a vector. It stores vectors that accompany the vector as different values of the markup element. The label is always a character, regardless of whether it is a number or character or Boolean in the input vector. They are useful in statistical modeling.

Use the factor () function to create a factor. The nlevels function gives a count of levels. You can try the levels () function and output what

# Create a vector.apple_colors <-C (' green ', ' green ', ' yellow ', ' red ', ' red ', ' red ', ' green ') # Create a factor Object.factor_apple <-Factor (apple_colors) # Print the Factor.print (factor_apple) print (Nlevels (factor_apple))

When the above code executes, it produces the following result:

[1] Green  green  yellow red    red    red    yellow green Levels:green red yellow# applying the Nlevels functi On we can know the number of distinct values[1] 3   
Data frame

A data frame is a tabular data object. Unlike a matrix in a data frame, each column can contain a model of different data. The first column can be a number, and the second column may be a character and the third column can be logical. It is equal to the length of the vector list.

The data frame is created using the data.frame () function.

# Create the data frame. BMI <-data.frame (gender = C ("Male", "Male", "Female"), height = c (All, 171.5, 165), weight = C (81,93,), age =c (42,38, ) Print (BMI)

When the above code executes, it produces the following result:

  Gender height weight Age1   Male  152.0     bayi  422   Male  171.5  165.0 383 Female  26  

----------------------------

Output function cat () function

R language Data type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.