R语言的学习

生信技能树-R语言系统的作业题-数据框的操作

2019-05-16  本文已影响0人  Ashu

听了生信技能树齐老师的课,对以前学习R语言起到复习和补充作用,补充的知识点有:

后续还有新内容及新作业...

作业1:

Albert_Einstein<-c(‘a’,‘b’,‘1’),c是什么作用,这里的变量是什么,有几个元素?

c的作用是函数,变量是Albert_Einstein,有3个元素(a,b,1)

创建1个含字符和数字的5个元素的向量(a,b,c,24,365),变量名为 r1
> r1 <- c('a','b','c','24','365')
> r1
[1] "a"   "b"   "c"   "24"  "365"
创建1个只含数字的5个元素的向量(24,365,60,5,12),变量名为r2
> r2 <- c(24,365,60,5,12)
> r2
[1]  24 365  60   5  12
判断r1中元素类型(is|class)
class(r1) 
#or
class(r1[1])
class(r1[2])
class(r1[3])
class(r1[4])
class(r1[5])

> class(r1)
[1] "character"
> class(r1[1])
[1] "character"
> class(r1[2])
[1] "character"
> class(r1[3])
[1] "character"
> class(r1[4])
[1] "character"
> class(r1[5])
[1] "character"
将r1中元素全部转为数值型
as.numeric(r1)
#or
r1 <- as.numeric(r1)
is.numeric(r1)

> as.numeric(r1)
[1]  NA  NA  NA  24 365
#Warning message:强制改变过程中产生了NA ,为以后对矩阵或数据框里的数据判断。
> r1 <- as.numeric(r1)
#Warning message:强制改变过程中产生了NA 
> is.numeric(r1)
[1] TRUE
判断r2中元素类型(is|class)
> class(r2)
[1] "numeric"
将r2中元素全部转为字符型
> as.character(r2)
[1] "24"  "365" "60"  "5"   "12" 
> is.numeric(r2)
[1] TRUE

作业2:

用seq函数创建一个含10个元素的向量,名为vec1
vec1 <- seq(from=1,to=10,by=1)
##or
vec1 <- seq(1,10,2)
vec1

> vec1 <- seq(from=1,to=10,by=1)
> vec1 <- seq(1,10,1)
> vec1
 [1]  1  2  3  4  5  6  7  8  9 10
计算vec1中位数、标准差、最小值、最大值
a <- median(vec1)
a
b <- sd(vec1)
b
c <- min(vec1)
c
d <- max(vec1)
d

> a <- median(vec1)
> a
[1] 5.5
> b <- sd(vec1)
> b
[1] 3.02765
> c <- min(vec1)
> c
[1] 1
> d <- max(vec1)
> d
[1] 10
用paste函数将vec1中的第3,5,7,9个元素x改为xvec
paste(vec1[c(3,5,7,9)],'vec',sep = '')
> paste(vec1[c(3,5,7,9)],'vec',sep = '')
[1] "3vec" "5vec" "7vec" "9vec"
用paste0函数将vec1中的第1,2,4个元素x改为xvec
paste0(vec1[c(1,2,4)],'vec')
> paste0(vec1[c(1,2,4)],'vec')
[1] "1vec" "2vec" "4vec"

作业3:

获取奇数行内容

先建一个矩阵

tmp<- matrix(1:24,nrow=4,ncol=6)
tmp

> tmp<- matrix(1:24,nrow=4,ncol=6)
> tmp
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    5    9   13   17   21
[2,]    2    6   10   14   18   22
[3,]    3    7   11   15   19   23
[4,]    4    8   12   16   20   24

e_t<-1:nrow(tmp)
tmp[e_t%%2==1,]

e_t<-1:nrow(tmp)
tmp[e_t%%2==1,]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 5 9 13 17 21
[2,] 3 7 11 15 19 23

######获取偶数行内容

e_t<-1:nrow(tmp)
tmp[e_t%%2==0,]

e_t<-1:nrow(tmp)
tmp[e_t%%2==0,]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 2 6 10 14 18 22
[2,] 4 8 12 16 20 24

######更改行名,更改列名

rownames(tmp) <- c('a','b','c','d')
colnames(tmp) <- c('one','two','three','four','five','six')
tmp

rownames(tmp) <- c('a','b','c','d')
colnames(tmp) <- c('one','two','three','four','five','six')
tmp
one two three four five six
a 1 5 9 13 17 21

b 2 6 10 14 18 22
c 3 7 11 15 19 23
d 4 8 12 16 20 24

课程分享

生信技能树全球公益巡讲

([https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g](https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g))

B站公益74小时生信工程师教学视频合辑

([https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw](https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw))

招学徒:

([https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw](https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw))
上一篇下一篇

猜你喜欢

热点阅读