當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。