生信星球培训第四十四期

Day 5 - Data structure

2020-03-17  本文已影响0人  咚_e4c6

LunaprimRose 2020.03.17

Day 5.png

Vector

原子型向量就是最简单的包含数据的向量

die <- c(1,2,3,4,5,6)
die
[1] 1 2 3 4 5 6
is.vector(die)    # is.vecrot 用来测试某个对象是否为原子型向量
[1] TRUE
five <- 5
five
[1] 5
is.vector(five)
[1] TURE
length(five)    # length 函数返回原子型向量的长度
[1] 1

Data Frame

数据框是列表的二维版本

df <- data.frame(face = c("ace","two","six"),
  suit = c("clubs","clubs","clubs"),value = c(1,2,3))
df
face  suit value
1  ace clubs     1
2  two clubs     2
3  six clubs     3

Sign system of R

可以通过 R 的记号体系来实现 R 对象中值的选取

head(deck)
   face   suit value
1  king spades    13
2 queen spades    12
3  jack spades    11
4   ten spades    10
5  nine spades     9
6 eight spades     8
deck[1,1]
[1] "king"
deck[1,c(1,2,3)]
  face   suit value
1 king spades    13
deck[c(1,1),c(1,2,3)]
    face   suit value
1   king spades    13
1.1 king spades    13
deck[-(2:52),1:3]
    face   suit value
1   king spades    13
deck[0,0]
data frame with 0 columns and 0 rows
deck[1,]
  face   suit value
1 king spades    13
deck[1,c(TRUE,TRUE,FALSE)]
  face   suit
1 king spades
row <- c(TRUE,F,F,F,F,F)
deck[row,]
  face   suit value
1 king spades    13
deck[1,c("face","suit","value")]
  face   suit value
1 king spades    13
deck[,"value"]
[1] 13 12 11 10  9  8
上一篇下一篇

猜你喜欢

热点阅读