array, matrix, list and dataframe

To summarize the "Getting Started 3R" (Reading, 'Riting,' Rrithmetic) in reading and writing, reading and writing at different data structures is still a little different.

vector

name

1
2
month.days<-c(31,28,31,30,31,30,31,31,30,31,30,31)
names(month.days)<-month.name

Operation text

1. text separation

1
2
pangram<-"The quick brown fox jumps over the lazy dog"
strsplit(pangram," ")

strplit()Function pangramwith spaces cut, the return value of this function is to list

1
words<-strsplit(pangram," ")[[1]]

An array of strings can be taken

2. Text Links

1
2
paste(LETTERS[1:5],1:5,sep="_",collapse="---")
paste("Sample",1:5)

A space connecting wordsthe elements, paste()the received parameter should be a plurality of variables, sepwe determine a connector between a plurality of vectors, and collapsedecide how to merge vector elements uniform.

3. Text Sort

1
sort(letters,decreasing=TRUE)

4. Find text

1
2
substr (state.name, Start = . 3 , STOP = . 6 ) 
grep ( "New" , state.name) #### by lookup mode

grep(pattern,x)The return is in line with the pattern element position in x

5. Text Replacement

1
gsub("cheap","sheep's","A wolf in cheap clothing")
1
2
x<-c("file_a.csv","file_b.csv","file_c.csv")
y<-gsub("file_","",x)

Factors classification

factor(x,levels,labels)R factor can be created, but levelsrefers to the input value of x, labelsrepresents the output value of the factor of the newly created.

Conversion factor

1
2
3
4
5
Numbers <-factor (C ( . 9 , . 8 , 10 , . 8 , . 9 )) 
STR (Numbers)
as.character (Numbers) ### returns the character elements
as.numeric (numbers) internal representation ### returns factor
as .numeric (as.character (Numbers)) ### return a numeric element

Ordered factor

Statistical data category
1
table(state.region)
Ordinal variables
  • Using the factor()function, and the specified parametersordered=TRUE
  • Use ordered()function

matrix

1
2
3
4
5
Matrix (Data, ncol, nrow, byrow) 
Dim () ### See matrix dimension
of rbind () ### rows by a vector matrix
cbind () ### columns by a vector matrix
cbind ( . 1 : . 3 , . 4 : . 6 , Matrix ( . 7 : 12 is , ncol = 2 ))

Index, modify and naming

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
first.matrix <- matrix [ . 1 : 12 is , ncol = . 4 , byrow = TRUE ] 
############# value
first.matrix [ . 1 : 2 , 2 : . 3 ]
first.matrix [ 2 : 3 ,] ### numeric index
first.matrix [- 2 , - 3 ] ### in addition to extracting the second row, third column all data
first.matrix [-C ( . 1 , 3 ),] ## to reduce the dimension of vector #
first.matrix [ 2 ,, drop = large column   Array, matrix, and List dataframe ERAL "> FALSE ] ### without reducing the dimensions, the matrix is still
############# modified
first.matrix [ . 3 , 2 ] <- . 4
first.matrix [ 2 ,] <- C ( . 1 , . 3 )
first.matrix [ . 1 : 2 , . 3 : . 4 ] <- C ( . 8 , . 4 , 2 , . 1 )
############# named ranks
rownames (X) <- C ( 'A' , 'B' )
colnames (X) < -C ( 'C' , 'D' )
colnames (X) [ . 1 ] <- 'AA'
X [ 'B' ,] ### as an index with the name

Compute

1
2
3
T () ### transposition
Solve () ### Inversion
X% *% T (X) ### is multiplied by

array

Vectors and matrices are arrays.

1
2
Array ( . 1 : 24 , Dim C = ( . 3 , . 4 , 2 )) ### to create a three-dimensional array
Dim (x) <- C ( . 3 , . 4 , 2 ) ### to change the dimension of the vector x

data.frame

Created by Matrix x.df<-as.data.frame(x)

Created by Vector data<-data.frame(x,y,z)

If the variable is a string type is created, R will be automatically converted into factor can be used stringAsFactor=FALSEto maintain string type

1
2
names (Data) [ 2 ] <- 'B'  ### named header
rownames (Data) <- C ( 'A' , 'B' , 'C' ) ### named observation

The value of the operation data.frame

data.frame并不是向量,而是一组向量列表。但是数据操作时可以当做矩阵来处理,访问单个变量时可以用$,访问多个变量时可以用[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#########修改值
y<-rbind(x,new.obs) ###添加单个观测
y<-rbind(x,'d'=new.obs) ###显式制定行名

new.obs<-data.frame(A=c(1,2),B=c(2,3))
rownames<(new.obs)<-c('e','f')
y<-rbind(x,new.obs) ###添加多个观测

x[c('e','f'),]<-matrix(c(1,1,2,4),ncol=2) ###使用索引添加多个值

##########修改变量
x$C<-new.var ###添加一个变量
new.df<-data.frame(newvar1,newvar2)
x<-cbind(x,new.df) ###添加多个变量

list

1
2
3
4
5
6
7
8
#######创建list
new.list<-list(x,y)###无命名列表
new.nlist<-list(name1=x,name2=y)###命名列表
names(new.nlist)###获取列表名称
length(new.list)###获取列表长度

########提取列表中的元素
###

提取列表中的元素

  • 使用[[]]返回元素本身
  • 使用[]返回选定元素的列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#########修改元素值
new.nlist[[1]]<-x
new.nlist[['name1']]<-x
new.nlist$name1<-x
new.nlist[1]<-list(x)
new.nlist[1:2]<-list(x,y)

##########移除元素
new.nlist[[1]]<-NULL
new.nlist[['name1']]<-NULL
new.nlist$name1<-NULL
new.nlist[1]<-list(NULL)

##########添加元素
new.nlist$name3<-z
new.nlist[['name3']]<-z
new.nlist['name3']<-list(z)

Synthesis list ##########
Z <-list (Z)
C (new.nlist, Z)

Guess you like

Origin www.cnblogs.com/lijianming180/p/12239692.html