Johns Hopkins University series of data Science courses--r language: Data types

Source: Internet
Author: User
Tags true true

1. Atomic objects

The R language has 5 basic types of atomic objects:

1) Character type character

The character object is included with "".

2) numeric type numeric (real numbers)

Numeric objects are numerically numeric (numeric) in R, and if you need to specify an integer type, you need to add L to the number after you change it. For example: The number 123 defaults to the numeric type, if it is required to be an integer type, can be represented as 123L.

3) Integer int

4) Complex Type complex

The real part of the complex number is expressed in real numbers, and the imaginary part is represented by real +i. such as: 3+2i,2+1i and so on.

5) logic type logical (TRUE/FALSE)

Note: There are several types of special objects in the R language:

    • Inf\-inf (infinity) indicates positive and negative infinity

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >> 1/0[1] inf> -1/0[1]–inf</span></span>

    • Na (not available) indicates missing values
NaN (not anumber) represents meaningless values
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >> 0/0[1] Nan</span></span>

2. Assignments and annotations

The assignment in R is represented by the pointing symbol "<-", but the SHIFT key is required to play the symbol "<", and the assignment symbol is made up of two characters, so it is very inconvenient to write code.

The R language is annotated with two "# #", # #后的代码不被执行, and the scope is valid within a row.

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >> x<-c # #y <-4 > X[1] 1 2 3> y error: Object ' Y ' is not found </span></span>

3. Vectors (vector )

The vectors in R can be created with the function C () (concatenate), and the object types within the same vector must be the same:

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >> x <-C (0.5, 0.6) # # numeric> x <-C (TRUE, FALSE) # # logical> x <-C (T, F) # # logical> x <-C ("A", "B", "C") # # character> x <-9:29 # # integer> x <-C (1+0i, 2+4i) # # Complex</span></span>

Vector can also be initialized with a function vector: vector ("datatype", length=length_of_vector)

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >> x<-vector ("integer", length=5) > x[1] 0 0 0 0 0> y<-vector ("character", length=5) > Y[1] "" "" "" "" "</span></span>

4. Minimum common characteristics (least common denominator)

In the same vector, if there are different types of objects, R does not give an error, but instead follows the low common attribute, converting the high type object to a low type.

<span style= "FONT-SIZE:18PX;" >> x<-c (true,2,1.2,3+2i, "Hello") > X[1] "TRUE"  "2" "     1.2"   "3+2i"  "Hello" ##> x<-c ( True,2,1.2,3+2i) > X[1] 1.0+0i 2.0+0i 1.2+0i 3.0+2i> x<-c (true,2,1.2) > X[1] 1.0 2.0 1.2> x<-c (TRUE,2) ; X[1] 1 2</span>

As can be seen from the above example, the basic object rank is ordered from high to Low: Logic, Integer, numeric, complex, character.


5. Type Strong turn

R casts the object type through the As.datatype (object) function, and the conversion principle also follows a high-level object that can be converted to a low grade, while a low-level object cannot be converted to a high level (data information is lost when converted).

<span style= "FONT-SIZE:18PX;" >> x <-0:6> Class (X) [1] "integer" > As.numeric (x) [1] 0 1 2 3 4 5 6> as.logical (x) [1] FALSE true TRU E true True true> as.character (x) [1] "0" "1" "2" "3" "4" "5" "6" > As.numeric (x) [1] na Na na warning message: na> X&L generated during forced change T;-3+2i> as.numeric (x) [1] 3 warning message: Discard imaginary part </span> when forced change


6. Matrix

The matrix is a two-dimensional array, the objects in the matrix have the same type, and the matrix can be created from the function matrix in the following format:

M<-matrix (vector,nrow=number_of_rows,ncol=number_of_columns,byrow=logic_value)

<span style= "FONT-SIZE:18PX;" >> M<-matrix (nrow=2,ncol=3) > M     [, 1] [, 2] [, 3][1,]   na   na   na[2,]   na   na   na > M<-matrix (1:6,nrow=2,ncol=3) > M     [, 1] [, 2] [, 3][1,]    1    3    5[2,]    2    4    6</ Span>

The matrix is populated by columns by default, and if you want the matrix to be populated by rows, you need to set the Byrow property to True.

<span style= "FONT-SIZE:18PX;" >> M<-matrix (1:6,nrow=2,ncol=3,byrow=true) > M     [, 1] [, 2] [, 3][1,]    1    2    3[2,]    4    5    6</span>

You can also create a vector and then add the objects in the vector to the specified matrix by using the Dim () function:

<span style= "FONT-SIZE:18PX;" >> a<-c (2,3) > a[1] 2 3> m<-c (1,2,3,4,5,6) > Dim (M) <-c (2,3) > M     [, 1] [, 2] [, 3][1,]    1< C25/>3    5[2,]    2    4    6</span>

The dim () function can also be used to view the dimensions of a matrix:

<span style= "FONT-SIZE:18PX;" >> Dim (m) [1] 2 3</span>

Matrices can also be combined into matrices using the function Cbind () and Rbind () functions:

<span style= "FONT-SIZE:18PX;" >> x<-c > Y<-c (4,5,6) > Z<-c (7,8,9) > Cbind (x, y)     x y[1,] 1 4[2,] 2 5[3,] 3 6> rbind (x, y )  [, 1] [, 2] [, 3]x    1    2    3y    4    5    6> rbind (x, Y, z)  [, 1] [, 2] [, 3]x    1    2    3y    4    5    6z    7    8    9</span>


7. List

A list is a special kind of vector that lists can contain different types of objects, even vector objects and matrix objects. Each object in the list is separated by commas, and each object index number is represented by a double "[[]]", and the elements in each object are represented by a single "[]".

<span style= "FONT-SIZE:18PX;" >> x<-list ("Hello", 1:4l,3.2,4+5i,true) > x[[1]][1] "Hello" [[2]][1] 1 2 3 4[[3]][1] 3.2[[4]][1] 4+5i[[5]][1] True</span>


8. Factor (factor)

Factors are generally used to represent categorical data, which can be either ordered or unordered. Using factor to indicate that categorical labels are more intuitive, such as using "male" "female" is better than using a quantity value like this.

<span style= "FONT-SIZE:18PX;"  >> x <-Factor (C ("Yes", "yes", "no", "yes", "no")) > X[1] Yes yes no  yes no levels:no yes> table (x) x No Yes   2   3</span>

The order of the objects in the levels can also be customized by parameter levels.

<span style= "FONT-SIZE:18PX;" >> x <-Factor (C ("Yes", "yes", "no", "yes", "no"), Levels=c ("Yes", "no") > X[1] Yes yes no  yes no Levels:ye S no</span>


9. Missing values

Is.na () is used to test whether the object is Na,is.nan () to test whether the object is Nan. Na is Nan, but Nan is not na,nan much deeper than NA.

<span style= "FONT-SIZE:18PX;" >> b<-c (1,2,3,na,4) > is.na (b) [1] false false  TRUE false> Is.nan (b) [1] false false FAL Se> b<-c (1,2,3,nan,4) > is.na (b) [1] false false  true false> Is.nan (b) [1] false false  True False</span>


10. Data frame

The data frame is used to store tabular data and is created with Data.frame (). You can treat a data frame as a special list collection, with the same length for each list and the same data type for the corresponding location.

<span style= "FONT-SIZE:18PX;" >> X<-data.frame (A=1:4,b=c ("A", "B", "C", "D")) > x  a b1 1 A2 2 B3 3 C4 4 d> nrow (x) [1] 4> Ncol (x) [1] 2& Lt;/span>


11. Naming

R can be named after the object by the function names (), by Dimnames () to the matrix, or by naming the list:

<span style= "FONT-SIZE:18PX;" >> x<-c (1,2,3,4)           # #给向量命名 > Names (x) <-c ("One", "one", "three", "four") > x one, and   three Four  1     2     3     4> names (x) [1] "one" "" "" "" Three "" Four   "> Y<-list (one=1,two=2,three=3   # #给列表命名 > y$one[1] 1$two[1] 2$three[1] 3> M<-matrix (1:4,nrow=2,ncol=2) > M     [, 1] [, 2][1,]    1< C12/>3[2,]    2    4> dimnames (m) <-list (C ("One", "B"), C ("Three", "Four")) > M    three Fourone     1    3two     2    4</span>
















Johns Hopkins University series of data Science courses--r language: Data types

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.