在 R 编程中获取和设置向量的长度 - length()函数:标题更改需要
R 编程语言中的length()函数用于获取或设置向量(列表)或其他对象的长度。
在 R 编程中获取对象的长度
在这里,我们将在 R 编程中获取向量的长度,为此我们将使用 length()函数。
Syntax: length(x)
Parameters:
- x: vector or object
示例 1:获取向量的长度
R
# R program to illustrate
# length function
# Specifying some vectors
x <- c(6)
y <- c(1, 2, 3, 4, 5)
# Calling length() function
# to get length of the vectors
length(x)
length(y)
R
A = matrix(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
# No of rows
nrow = 3,
# No of columns
ncol = 3,
# By default matrices are in column-wise order
# So this parameter decides how to arrange the matrix
byrow = TRUE
)
print(A)
# length of A
length(A)
R
print(BOD)
length(BOD)
R
# R program to create a List and get the len
# The first attributes is a numeric vector
# containing the employee IDs which is created
# using the command here
empId = c(1, 2, 3, 4)
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
print(empList)
print("Length of the list:")
length(empList)
R
# R program to split a string
# Given String
string <- "Geeks For Geeks"
# Basic application of length()
length(string)
# unlist the string and then count the length
length(unlist(strsplit(string, "")))
R
# R program to illustrate
# length function
# Specifying some vectors
x <- c(3)
y <- c(1, 2, 3, 4, 5)
z <- c(1, 2, 3, 4, 5)
# Setting length of the vector
length(x) <- 2
length(y) <- 7
length(z) <- 3
# Getting elements of the
# new vectors
x
y
z
输出 :
[1] 1
[1] 5
示例 2:获取矩阵的长度
R
A = matrix(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
# No of rows
nrow = 3,
# No of columns
ncol = 3,
# By default matrices are in column-wise order
# So this parameter decides how to arrange the matrix
byrow = TRUE
)
print(A)
# length of A
length(A)
输出:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
9
示例 3:获取 Dataframe 的长度
这里 BOD 是数据框,有 6 行 2 列,在水质评估中给出了生化需氧量与时间的关系。我们将得到这个数据帧的长度。
R
print(BOD)
length(BOD)
输出:
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
2
注意:如果参数是矩阵或数据框,则返回变量的数量:
示例 4:获取列表的长度
R
# R program to create a List and get the len
# The first attributes is a numeric vector
# containing the employee IDs which is created
# using the command here
empId = c(1, 2, 3, 4)
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
print(empList)
print("Length of the list:")
length(empList)
输出:
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[3]]
[1] 4
[1] "Length of the list:"
3
示例 5:获取字符串的长度
在 R 语言中,我们不能轻易得到字符串的长度,首先,我们必须使用 split 获取字符串的字符,然后将每个字符取消列出来计算长度。
R
# R program to split a string
# Given String
string <- "Geeks For Geeks"
# Basic application of length()
length(string)
# unlist the string and then count the length
length(unlist(strsplit(string, "")))
输出:
1
15
在R编程中设置对象的长度
在这里,我们将在 R 编程中设置向量的长度,为此我们将使用 length()函数。
Syntax: length(x) <- value
Parameters:
- x: vector or object
R
# R program to illustrate
# length function
# Specifying some vectors
x <- c(3)
y <- c(1, 2, 3, 4, 5)
z <- c(1, 2, 3, 4, 5)
# Setting length of the vector
length(x) <- 2
length(y) <- 7
length(z) <- 3
# Getting elements of the
# new vectors
x
y
z
输出:
[1] 3 NA
[1] 1 2 3 4 5 NA NA
[1] 1 2 3