其他分享
首页 > 其他分享> > R语言(矩阵的名字,数组,列表,数据框)

R语言(矩阵的名字,数组,列表,数据框)

作者:互联网

添加与修改矩阵的名字
> dimnames(vec) <- list(c("e","f"),c("a","b","c")) 先行后列 > vec a b c e 1 3 5 f 2 4 6 > a [1] 2 3 4 > names(a) <- ("a") 给向量加名字 > a a <NA> <NA> 2 3 4 > colnames(vec) <- c("w","j","w") 加列名 > vec w j w e 1 3 5 f 2 4 6 > rownames(vec) <- c("t","f") 加行名 > vec w j w t 1 3 5 f 2 4 6 > list(c("e","f"),c("a","b","c")) [[1]] [1] "e" "f" [[2]] [1] "a" "b" "c" > ?matrix > as.matrix(vec) w j w t 1 3 5 f 2 4 6 > as.matrix(a) [,1] a 2 <NA> 3 <NA> 4 >

  添加与修改列表的名字

> list(a,b,vec)
[[1]]
   a <NA> <NA> 
   2    3    4 

[[2]]
[1] 6 7 8

[[3]]
  w j w
t 1 3 5
f 2 4 6

list(a=a,b=b,c=vec)
$a
   a <NA> <NA> 
   2    3    4 

$b
[1] 6 7 8

$c
  w j w
t 1 3 5
f 2 4 6

>

> names(x)[1] <- ("new1")
> x
$new1
a <NA> <NA>
2 3 4

$b
[1] 6 7 8

$c
w j w
t 1 3 5
f 2 4 6

单独修改列表

列名

  

 wa <- data.frame(state.abb,state.area)
> wa
   state.abb state.area
1         AL      51609
2         AK     589757
3         AZ     113909

新建数据框
 fa <- factor(state.abb)
> fa
 [1] AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE
[28] NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY
50 Levels: AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN ... WY
> fb <- state.abb
> fb
 [1] "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "FL" "GA" "HI" "ID" "IL" "IN" "IA" "KS"
[17] "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE" "NV" "NH" "NJ" "NM" "NY"
[33] "NC" "ND" "OH" "OK" "OR" "PA" "RI" "SC" "SD" "TN" "TX" "UT" "VT" "VA" "WA" "WV"
[49] "WI" "WY"
> table(fb)
fb
AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE 
 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 
NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY 
 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 
> 

  因子(factor)与统计(table)

factor可以作为分级来看,使用图标可以统计每一类出现的次数

对于其他数据格式,则可以通过table来统计,但只有转换成因子才能画图。

数据框与矩阵的区别

数据框由多种数据格式的向量组成,矩阵只有一种,但矩阵可以四则运算。

 

标签:GA,AK,矩阵,AL,列表,vec,数组,AZ,WY
来源: https://www.cnblogs.com/AlanNote/p/15135256.html