R language Data type and object type _r language

Source: Internet
Author: User


Data type: The type of element value within a variable. Mainly includes: numerical type, character type, logical type, plural type.

Object Type: R language organizes and manages different ways of internal elements. Mainly include: vectors, matrices, arrays, lists, data boxes, factors, time series.

Data types Let's give you a brief introduction.

Numeric type: The value is a real number, which is often expressed in the R locale.

Plural: Values can be extended to imaginary numbers.

Logical type: Evaluates to True (T) or flase (F).

Character type: The value is a string.

#数值型
a<-9.11
Mode (a)
#复数型
a<-9.11+9.11i
Mode (a)
#字符型
a<-' 9.11 '
Mode (a)
#逻辑型
A<-t
Mode (a)


Note, however, that there are two situations that cannot be described with the above 4 types of data, namely the missing (NA) of the data and the unknown state of the data (NULL).

NA: Occupy the data space, participate in the operation

NULL: Does not occupy any data space

X<-c (10,20,30,40,na)
Mean (x)
Mean (x,na.rm = T)
Length (x)
Y<-c (10,20,30,40,null)
Mean (y)
Length (y)


How to discriminate between Na and null.

The first element of the vector subscript in is.na:r language is 1, and if there is Na, return the corresponding true, otherwise it is flase

Is.null: When processing vectors, the internal data is not judged separately, but the vector is judged uniformly as a data, and the return value is only one flase.

X<-c (10,20,30,40,na)
Y<-c (10,20,30,40,null)
Is.na (x)
Is.null (y)



Here we describe the object types in the R language.

The R language uses attributes to assist in describing each object's information, intrinsic properties: pattern (mode) and length. Learn about the rest of the properties by using the attributes () and attr () functions.

Df<-data.frame (A=c (1:10), B=sample (C (' F ', ' M '), size=10,replace=t))
Attributes (DF) #展示列名, row name, data type
attr (DF, ' names ') #展示自己想要的结果 (column name, row name, data type)
attr (DF, "names") [1]<-' New_name ' #重命名列名
attr (DF, "Row.names") <-letters[1:10] #可以重命名行名
Df


Vector

Vector index

# Create Vectors
X<-1:10
Y<-c (' f ', ' m ', ' f ', ' m ', ' T ', ' f ')
Z<-c (t,f,f,f,t)
In the R language, C () is used to create vectors, and the elements in the vector are separated by commas. This is very simple, I believe that everyone has mastered the very skilled.

So let's simply recall that there are several ways to index a vector.

toothgrowth# Data Set
TOOTHGROWTH[1] #下标索引
toothgrowth[' Len '] #名字索引
Toothgrowth[,c (t,f,f)] #T/F Index
Split (toothgrowth$len,f = Toothgrowth$supp) #split分割 (using regular expressions)

Toothgrowth[which (Names (toothgrowth) = "Len")] #which索引
A<-c (11,12,13,14)
A[which (a>12)] #which返回的是元素下标
A[which.min (a)]
A[which.max (a)]

Subset (Toothgrowth,select = 1) #subset索引
Subset (Toothgrowth,select = C (t,f,f))
Subset (toothgrowth,select= ' Len ')

Toothgrowth[grep (pattern = ' Len ', x = names (toothgrowth))] #grep索引

B<-c (11,12,13,14,15,16,17,18,19,20)
C (11,21)%in% B #%in% Index
D<-c (11,21)
Match (D,B) #match索引


In fact, there are many ways to help us locate each element of the vector, the reader can think for themselves. In addition, because space is limited, the reader can copy the above code to run on its own R compiler, where the results are not shown.

Vector editing
A<-c (1:10)
B<-LETTERS[1:10]
A<-c (a,b) # vector extension
A

Deletion of elements in vectors

A<-c (1:10)
B<-LETTERS[1:10]
A<-c (A,B)
A[-c (5:9)] #删除多个向量

Ordering of elements in a vector

A<-c (10:1,11:20)
Sort (a)
Rev (a)
Rev (sort (a))

Readers should be aware of the differences between the three sorting methods mentioned above.

Vector to weight

A<-c (1:10,5:15,10:20)
A
Unique (a)
As.numeric (Names (table (a)))

Processing of missing values in vectors

A<-c (na,na,1,5,6,8,7,9)
A[!is.na (a)]
Na.omit (a)
Na.fail (Data.frame (a)) #判断数据框中有没有缺失值
Na.fail (Na.omit (a))

Vector size Comparison

A<-c (3,7,8,5,4,2,5)
B<-c (4,7,8,9,5,7,5)
C<-c (8,7,5,4,8,5,7)
Pmin (A,B,C)
Pmax (A,B,C)
Pmin: This compares the vector ABC counterpart elements and makes the smaller elements a new vector.

Pmax: The larger elements are composed of new vectors.


Intersection, set and complement of vectors

Intersect (A,B): Returns a vector-------> intersection of elements that belong to both A and B.

Union (A,B): Make a new vector--------> set of all the elements in A and b

Setdiff (A,B): Returns a new vector-----> complement consisting of elements that do not belong to a or b

A<-c (3,7,8,5,4,2,5)
B<-c (4,7,8,9,5,7,5)
Intersect (A,B)
Union (A,B)
Setdiff (A,B)

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.