首页 > 编程语言 > 详细

2-2 R语言基础 向量

时间:2018-11-11 15:45:25      阅读:160      评论:0      收藏:0      [点我收藏+]

#Vector 向量的三种创建方法,两个参数:类型,长度

> x <- vector("character",length=10)
> x1 <- 1:4
> x2 <- c(1,2,3,4)
> x3 <- c(TRUE,10,"a") #如果给向量赋值时元素类型不一致,R就会强制转换,将他们变为同一类型
> x4 <- c("a","b","c","d")


> #强制转换的函数如下:
> as.numeric(x4)
[1] NA NA NA NA
Warning message:
NAs introduced by coercion


> #强制转换的函数如下:
> as.numeric(x4)
[1] NA NA NA NA
Warning message:
NAs introduced by coercion


> as.logical(x4)
[1] NA NA NA NA


> as.character(x4)
[1] "a" "b" "c" "d"


> class(x1) #查看数据的类型
[1] "integer"


> names(x1) <- c(x4) #给向量每一个元素添加名称

2-2 R语言基础 向量

原文:https://www.cnblogs.com/hankleo/p/9942098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!