当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 structure()用法及代码示例


structure()R语言中的函数用于通过改变维度来获取或设置向量的结构。

用法: structure(vec, dim)

参数:
vec:向量
dim:新维度

范例1:


# R program to get
# the structure of a Vector
  
# Creating a Vector
# of Numbers
x1 <- c(1:10)
structure(x1)
  
# Creating a vector 
# of strings
x2 <- c("abc", "cde", "def")
structure(x2)

输出:

[1]  1  2  3  4  5  6  7  8  9 10
[1] "abc" "cde" "def"

范例2:


# R program to change 
# the structure of a Vector
  
# Creating a Vector
x <- c(1:10)
x
  
# Changing the structure of vector
y1 <- structure(x, dim = c(2, 5))
y2 <- structure(x, dim = c(5, 2))
y1
y2

输出:

 [1]  1  2  3  4  5  6  7  8  9 10
     [, 1] [, 2] [, 3] [, 4] [, 5]
[1, ]    1    3    5    7    9
[2, ]    2    4    6    8   10
     [, 1] [, 2]
[1, ]    1    6
[2, ]    2    7
[3, ]    3    8
[4, ]    4    9
[5, ]    5   10

相关用法


注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Get or Set the Structure of a Vector in R Programming – structure() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。