👉 Vector in R = 1d-array in Python@Numpy
c(2,3,4) # [1] 2 3 4
2:4 # [1] 2 3 4
numeric(3) # [1] 0 0 0
c()
2:5
返回由从2到5的整数组成的向量numeric(10)
初始化一个长度为10的zero vector⚠️ 和Python不同的是,R不是嵌套vector!
>>> c(c(1,2), c(3,4))
[1] 1 2 3 4
如果是python,则结果是[[1,2], [3,4]]
而不是[1,2,3,4]
You can give a name to the elements of a vector with the names()
function.
unname(x)
returns a copy of x without namesnames(x) <- NULL
removes the names in x>>> poker_vector <- c(140, -50, 20, -120, 240)
>>> poker_vector
[1] 140 -50 20 -120 240
>>> names(poker_vector) <- c("Mon", "Tue", "Wed", "Thu", "Fri")
>>> poker_vector
Mon Tue Wed Thu Fri
140 -50 20 -120 240
>>> poker_vector[1]
Mon
140
>>> poker_vector[c(1,5)]
Mon Fri
140 240
>>> poker_vector[1:3]
Mon Tue Wed
140 -50 20
>>> poker_vector[c("Mon","Fri")]
Mon Fri
140 240
>>> poker_vector[c(TRUE, FALSE, TRUE, FALSE, FALSE)]
Mon Wed
140 20
>>> length(poker_vector)
[1] 5