美文网首页R语言的学习
R语言简单的运行

R语言简单的运行

作者: Ashu | 来源:发表于2019-04-23 17:29 被阅读0次

    以前没有接触过R语言,第一天学习R语言,生信技能树团队的齐老师安排作业给我,我觉得有作业才有目标性和动力去搜索资源。通过网络搜索和参考相关书籍以及齐老师的指导,顺利地完成作业:
    1.创建一个向量元素为a,b,e,2,5

    work <- c(a,b,e)
    data <- c(2,5)
    

    2.确定上述向量中每个元素的数据类型

    > class(work[1])
    [1] "character"
    > class(work[2])
    [1] "character"
    > class(work[3])
    [1] "character"
    > class(data[1])
    [1] "numeric"
    > class(data[2])
    [1] "numeric"
    

    3.创建一个包含字符和数值的数据框(5行3列)

    patientID <- c(1,2,3,4,5)
    age <- c(25,32,38,43,60)
    breastcancer <- c("Type1","Type1","Type3","Type1","Type2")
    patientdata <- data.frame(patientID,age,breastcancer)
    patientdata
      patientID age breastcancer
    1         1  25        Type1
    2         2  32        Type1
    3         3  38        Type3
    4         4  43        Type1
    5         5  60        Type2
    

    4.对上述数据框的行名和列名进行更改

    rownames(patientdata) <- c("a","b","c","d","e")
    colnames(patientdata) <- c("personalities","years","cancer")
    patientdata
      personalities years cancer
    a             1    25  Type1
    b             2    32  Type1
    c             3    38  Type3
    d             4    43  Type1
    e             5    60  Type2
    

    5.确定数据框中各列的数据类型

    class(patientdata[,1])
    class(patientdata[,2])
    class(patientdata[,3])
    > class(patientdata[,1])
    [1] "numeric"
    > class(patientdata[,2])
    [1] "numeric"
    > class(patientdata[,3])
    [1] "factor"
    

    or

    class(patientdata$personalities)
    class(patientdata$years)
    class(patientdata$cancer)
    > class(patientdata$personalities)
    [1] "numeric"
    > class(patientdata$years)
    [1] "numeric"
    > class(patientdata$cancer)
    [1] "factor"
    

    6.不同方法检索数据框的第3行第2列的元素

    patientdata[3,2]
    patientdata['c','years']
    > patientdata[3,2]
    [1] 38
    > patientdata['c','years']
    [1] 38
    

    7.创建一个矩阵(4行6列)

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

    8.对上述矩阵的行名和列名进行更改

    colnames(Y)<-c("R1","R2","R3","R4")
    rownames(Y)<-c("C1","C2","C3","C4","C5")
    
     R1 R2 R3 R4
    C1  1  6 11 16
    C2  2  7 12 17
    C3  3  8 13 18
    C4  4  9 14 19
    C5  5 10 15 20
    

    9.不同方法检索数据框的第3行第2列的元素

    Y[3,2]
    Y["C3","R2"]
    
    > Y[3,2]
    [1] 8
    > Y["C3","R2"]
    [1] 8
    

    将作业写至简书邮件jmzeng1314@163.com,抄送juanyuanjing@1163.com

    相关文章

      网友评论

        本文标题:R语言简单的运行

        本文链接:https://www.haomeiwen.com/subject/ecbmgqtx.html