R语言的数据结构介绍"##"代表输出内容
向量
储存一维数据、其中数据数据类型相同、用C()创建向量。
例:
v.number<- c(1,2,3,4,5,6)
v.char<- c(”tom”,”jack”,”mack”,”anna”)
v.bool<- c(TRUE,FALSE,FALSE)
例1(真假问题):
if(-1) print("true") #-1为真
##[1] true
if(0) print("false") #无输出
if() print("fasle") #报错,无法比较
##Error:missing value where TRUE/FALSE needed
if(NULL) print("false")#报错,无法比较
##Error:atgument is of length zero
例2(数据类型转换问题):
x<- c(1:10,"jane")
x
##[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
[11] "jane"
例3(数据类型转换问题):
x<-c(1:10,"jace")
class(x)
##[1] "character"
y<-c(1:10)
class(y)
##[1] "integer"
数据转换类型次序:NULL->raw->logical->integer->complex->character->list->expression
例4(下标问题):
x<-c(1:10)
x[0]
##integer(0)
x[1]
##[1] 1
x[2:4]
##[1] 2 3 4
x[-1]
##[1] 2 3 4 5 6 7 8 9 10
x[11]
##[1] NA
变量命名问题:如x,x.1,"1",.x,.
R语言数据结构实例入门
最新推荐文章于 2025-03-08 22:21:41 发布