Second record of R language learning

data type

1. Assignment operation: x < - 10 x is the variable name, < - is the assignment symbol, 10 is the value (R language is different from C, JAVA, Python) "=" is also OK, but the efficiency is not as high as

2. Data members: character type, numerical type, boolean type, complex type, default value
The discrimination of data type is. And the transformation as;

3. View the type of data box
mode, class and typeof are preferred
Accuracy: mode < class < typeof
The mode function only looks at the large classes of data. The class function looks at the classes of data. The typeof function is more detailed. It looks at the system of data.

4, vector

a<-c(1,2,33) # C function of vector
is.character(a)#judge
is.numeric(a)
b<-as.character(a)#Transformation
b<-1+3i#complex
is.complex(b)

#Create a data box with three different types of object members

> data<-data.frame(c1=c(1,2,3),c2=1:3,c3=T)
> sapply(data,mode)#Pass two parameters
       c1        c2        c3 
"numeric" "numeric" "logical" 
> sapply(data,class)
       c1        c2        c3 
"numeric" "integer" "logical" 
> sapply(data,typeof)
       c1        c2        c3 
 "double" "integer" "logical" 
# It can be seen that the accuracy mode < class < typeof

x<-c(T,T,F)
y<-c(F,T,F)
x&&y#Only the first initial FALSE will be compared
x&y#Will output false true false for each

x<-c(1,0,3,4) 
is.na(x) #FALSE FALSE FALSE FALSE
is.null(x) # FALSE

In R language, NA means that the value on the location is empty, NULL means that there is no location, and the variable is empty.
Judgement sentence
To determine whether an element in a vector has no value: is.na()
Judge whether the vector is null or not: is.null()

1. R has the object type to store data, including vector, matrix, array, data box, list

Vector creation: it can be numerical type, character type, boolean type and complex type according to the management method of one-dimensional array
You can use = or you can use the C function,
Note: all elements of a vector must be of the same type. If not, R will cast!

x<-seq(1:7)#Creating numeric vectors 1, 2, 3, 4, 5, 6 are created in two ways
x<-c(1,2,3,4,5,6)
y<-c("l","o","v","e")#Create character vector l, o, v, e
x<-c(TRUE,FALSE,FALSE,TRUE)#Create Boolean vectors TRUE,FALSE,FALSE,TRUE

2. mode function

3. One of the most powerful aspects of R language is Vectorization
Arithmetic operation of vector

seq function
The basic form of a series that produces equidistant intervals is:
seq(from=1,to=1,by=((to-from)/length.out-1),along.with=null...)
Left closed right open!

seq(1,11,by=2)#First and last data 11 and step 2 are given
seq(1,-11,length=7)#Step size is (- 11-1) / (7-1) = - 2

x3<-seq(4,by=2,length=5)
x4<-seq(by=3,along.with = x3)
print(x4)
#Get 1 4 7 10 13

rep is a repetition function. The basic form is rep (x, n) x is the sequence to be repeated, and N is the number of repetitions

rep(1:4,3)#Repeat the sequence 1, 2, 3, 4 3 times to output: 1 23 4 1 2 3 4 1 2 1 2 3 4 3 4
rep(1:4,each=2) #Repeat each element in the 1, 2, 3, 4 sequence twice respectively. Output: 1 12 2 3 3 4 4
rep(3:2,each=2,len=3) #Each element in sequence 3,2 is repeated twice respectively, and the length of the generated sequence is specified as 3 output: 3 32
rep(1:4,c(2,1,2,1),len=3)#Can be specified to control the repetition output: 1 2 3
#Len and each are used together. In the absence of each, len=3 will be recognized first
#time should be used with each

Index vector
(from 1)

ve<-c(1,2,3,4,5,6)
ve[c(2,4,5)] #View the values of elements 2,4,5

ve<-c(1,2,3,42,509,688)
which(ve>3)#Positions of the first three values 4, 5, 6
ve[which(ve>3)] #The returned value is 42509688
#It can also be deleted
ve[-c(1,2,3)]


ve<-c(1,2,3,4,5,6)
ve[c(FALSE,TRUE,FALSE,TRUE,TRUE,FALSE)]#Access with Boolean sequence


ve<-c(1,2,3,4,5,6)#An index in a vector equal to 1 or 3
which(ve==1|ve==3) 

ve<-c(1,2,3,4,5,6)
vc<-c(1,2,3,4,5) #Judge whether the vector contains the data in (1,5) vector?
ve%in%vc # Use in function   # [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
ve[ve%in%vc]  # [1] 1 2 3 4 5 back to vector index


which function
Will return TRUE position in Boolean vector

sort function
Increasing the order of arrangement, TRUE for descending, F for ascending

x<-c(5,6,8,7,4,1,9)
sort(x,decreasing = T)#Descending order
Published 2 original articles, praised 0 and visited 7
Private letter follow

Keywords: R Language Java Python

Added by fred2k7 on Wed, 11 Mar 2020 12:21:10 +0200