And Name Complete language vector R

First, to be noted that, in the R vector and a list of the language is not the same, each element of the vector type is the same, and conversely the list, where the list of lists in Python somewhat similar. The following is a classification:

The same types of data Different types of data
One-dimensional Vector (vector) List (lists)
Two-dimensional Matrix (matrics) Data block (dataframe)
N-dimensional Array (array)

Block 1: generating vectors

R major languages ​​used to generate vectors are several methods:

Method 1: Use cFunction

We know that if the vector elements inside should be the same type, so if we do not use the same type of data generated at the time what will happen? for example:

######## 第一种情况
x <- c('abc',123)
x

# 输出
"abc" "123"

####### 第二种情况
x <- c(123,False)
x

# 输出
123 0

####### 第三种情况
x <- c("abc", 123, False)
x

# 输出
"abc" "123" "False"

Observation above code, in conjunction with the storage space character, numeric, logical greatest for the character type, we can conclude that: when the data type is not the same, the data will need more storage space transition type , which is like fat thin can wear the clothes, but not vice versa.

Method 2: Use :symbol generating vectors

This method is relatively simple, but more explanation, see for example:

######
x <- 1:3
x

# 结果
1 2 3

######
x <- 3:1

# 结果
3 2 1

This approach vector is generated such as 1step length, then if you want to generate unequal step how to do it? You can use the following method

Method three: Using seq()function

# 使用方法
seq(from, to, by=) 或 seq(from, to, length=)
其中:
	from表示序列的开始值
	to表示序列的终止值
	by表示步长,如果没有设定,默认值是by=1
	length指的是序列的总长度,也可以写成length.out

# 例子1
seq(1,10,by=2)
[1]1 3 5 7 9

# 例子2
seq(1,10,length=5)
[1]1.00 3.25 5.50 7.75 10.00

Method 4: Using the rep()function

This function corresponds to the English words: Repetition, the n-repeat.

# 使用方法
rep(data,times,each=)
其中:
	data表示需要重复的元素
	times表示重复次数,是data整体重复几遍
	each表示每个元素重复的次数,是单个元素重复几遍

# 例子1
rep(1:2,3)
[1]1 2 1 2 1 2

# 例子2
rep(1:2,each=3)
[1]1 1 1 2 2 2

Method 5: Using the pastefunction

# 使用方法
paste(A,B,sep="")
将A字符串和B字符串连接成新字符串,也可以将两个以上的字符串连接
sep的默认值是一个空格

# 例子
paste("jinrong","1401","sep"="")
[1]"jinrong1401"

paste("jinrong","1401")
[1]"jinrong 1401"

Block 2: Vector named

Method 1: When naming a direct assignment

x <- c(x=3, y=4, z=5)

# 输出
x y z
3 4 5

This method is equivalent to setting a map, it can be used to batch naming variables, very easy to use, here is not to say

If we give the variables renamed how to do it? Consider using names()function

Method 2: Using names()Function

# 使用方法:
names(x) <- value
# 例子
names(x)[2] <- "U"
# 输出
x U z
3 4 5

Exercise

QUESTION: How the above mentioned method produces the following vector
"Finance A 1701", "1701 Finance B" "A financial 1702" "1702 Finance B"
"A financial 1703" "1703 Finance B" "A financial 1704" "Financial 1704 B "

参考代码:
a <- "金融"
b <- 1701:1704
c <- c("甲","乙")

bc = rep(0,length(b)*length(c))
i = 1
for (year in b){
  bc[i] = paste(a,paste(year,"甲",sep=""),sep="")
  bc[i+1] = paste(a,paste(year,"乙",sep=""),sep="")
  i = i+2
}

# 输出
[1] "金融1701甲" "金融1701乙" "金融1702甲" "金融1702乙"
[5] "金融1703甲" "金融1703乙" "金融1704甲" "金融1704乙"
Published 10 original articles · won praise 25 · views 5949

Guess you like

Origin blog.csdn.net/qq_41196612/article/details/104630690