老码农学生信

R数据结构小结

2019-06-17  本文已影响0人  Angeladaddy

1. 向量(vector)

a=c(1,2,3,4,5)
b=c(1:10)
d= 1:10
e=c(TRUE,FALSE,FALSE,TRUE)
f= letters

2. 数组(array)

array(data = NA, dim = length(data), dimnames = NULL)
as.array(x, ...)
is.array(x
three_d_array <- array(  1:24,  
dim = c(4, 3, 2),  
dimnames = list(    
  c("one", "two", "three", "four"),    
  c("ein", "zwei", "drei"),    
  c("un", "deux")  ) )

3. 矩阵(matrix)

matrix(data = NA, nrow = 1, ncol = 1, 
  byrow = FALSE,dimnames = NULL)

as.matrix(x, ...)
## S3 method for class 'data.frame'
as.matrix(x, rownames.force = NA, ...)
a_matrix <- matrix(   
  1:12,   
  nrow = 4,     #ncol = 3 也是同样的效果   
  dimnames = list(     
    c("one", "two", "three", "four"),     
    c("ein", "zwei", "drei")   
    ) 
  )

4. 列表(list)

list(...)
pairlist(...)

as.list(x, ...)
## S3 method for class 'environment'
as.list(x, all.names = FALSE, sorted = FALSE, ...)
as.pairlist(x)

is.list(x)
is.pairlist(x)

alist(...)
a_list <- list(  
  c(1, 1, 2, 5, 14, 42),  # 第1项
  month.abb,  # 第2项
  matrix(c(3, -8, 1, -3), nrow = 2),  # 第3项
  asin # 第4项
 )

5. 数据框(dataframe)

data.frame(..., row.names = NULL, check.rows = FALSE,
           check.names = TRUE, fix.empty.names = TRUE,
           stringsAsFactors = default.stringsAsFactors())

default.stringsAsFactors()
a_data_frame <- data.frame(   
x = letters[1:5],  
y = rnorm(5),   
z = runif(5) > 0.5 
)

注意每项(每列)的长度都是5

上一篇 下一篇

猜你喜欢

热点阅读