生信星球?qū)W習(xí)小組第29期第五天

5種基本類型的對象

字符character

雙引號或單引號夾起來,如“字符”

賦值 orange 為字符串 red、green、yellow,顯示字符串,查看字符串?dāng)?shù)據(jù)類型,計算字符串長度。

# Create a vector
orange <- c('red','green',"yellow")
print(orange)
[1] "red"    "green"  "yellow"

# Get the class of the vector
print(class(orange))
[1] "character"

length(orange)
[1] 3
證書integer
復(fù)數(shù)complex

如x+ab

邏輯logical

true/false

邏輯假和真的幾種形式,查看邏輯假的變量類型。

# 假
F / FALSE / 0

F       
[1] FALSE

# 真
T / TRUE / !0

class(T)
[1] "logical"
數(shù)值numeric

整數(shù)、小數(shù)、科學(xué)計數(shù)的方式,默認(rèn)雙精度型數(shù)據(jù),real numbers

6種儲存數(shù)據(jù)的對象類型:

因子factor

分類型數(shù)據(jù)需要把數(shù)據(jù)分為不同的水平,如性別分為男女兩個因子

向量vector

有相同基本類型元素組成的序列,相當(dāng)于一維數(shù)組

賦值變量 x 為 1,2,3 / 1,2,3,4,5,6,7,8,9; 取值 2,3 / 3 / 3,2,1; 輸出字符串和變量(myString) Hello World; 輸出 3.9 與 1.6 之和。

x<-c(1,2,4) / x<-c(1:9) / x[c(0,0,1)]
x
[1] 1 2 4
c(1,2,4)->x
x
[1] 1 2 4
x[2:3] / x[3] / x[c(3,2,1)]
[1] 2 4
print("Hello World")
[1] Hello World  
# Add two numbers
print(3.9 + 1.6)
[1] 5.5
myString <- "Hello, World!"
print ( myString) / cat(myString,"\n")
[1] "Hello, World!" 

賦值隱藏變量 a 為 2,3,4,var_123 為 123;顯示所有變量 、隱藏變量;正則 var*;刪除 隱藏變量 a;顯示所有變量。

.a<-c(2,3,4)
var_123=123
ls()
[1] "a"       "var_123"
ls(all.name=TRUE)
[1] ".a"      "a"       "var_123"
ls(pattern="var")
[1] "var_123"
rm(.a)/remove(.a)
ls(all.name=T)
[1] "a"       "d"       "var_123"
矩陣matrix

以 1,2,4,8(cells、rnames、cnames) 構(gòu)建矩陣 mymatrix
C1 C2
R1 1 2
R2 4 8

cells<- c(1,2,4,8)
rnames<-c("R1", "R2")
cnames<-c("C1", "C2")  
mymatrix<-matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames))
mymatrix 
#       C1   C2
# R1   1     2
# R2   4     8
矩陣
列表list

向量、矩陣和數(shù)組的元素必須是同一類型的數(shù)據(jù),但是如果一個數(shù)據(jù)對象需要含有不同的數(shù)據(jù)類型,則采用列表

g(title) 為 My First List, h(ages) 為 25, 26, 18, 39, j 為 1~10(5行)的矩陣,k 為字符串 one, two, three 建數(shù)據(jù)列表 mylist;

g<-"My First List"
h<-c(25, 26, 18, 39)
j<-matrix(1:10, nrow=5)
k<-c("one", "two", "three")
mylist<-list(title=g, ages=h, j, k)
mylist

$title
[1] "My First List"

$ages
[1] 25 26 18 39

[[3]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

[[4]]
[1] "one"   "two"   "three"
列表
數(shù)組array

一維數(shù)組是向量,二維數(shù)組是矩陣。向量和矩陣的推廣

數(shù)據(jù)框data.frame

一種矩陣形式的數(shù)據(jù),可以是不同類型的數(shù)據(jù)。

patientID 為 1, 2, 3, 4,age 為 25, 34, 28, 52, diabetes 為 Type1, Type2, Type1, Type1,status 為 Poor, Improved, Excellent, Poor 建數(shù)據(jù)框 patientdata。

# method 2
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
head(patientdata)
#  patientID age diabetes    status
#1         1  25    Type1      Poor
#2         2  34    Type2  Improved
#3         3  28    Type1 Excellent
#4         4  52    Type1      Poor
數(shù)據(jù)框
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這是16年5月份編輯的一份比較雜亂適合自己觀看的學(xué)習(xí)記錄文檔,今天18年5月份再次想寫文章,發(fā)現(xiàn)簡書還為我保存起的...
    Jenaral閱讀 3,162評論 2 9
  • 指針是C語言中廣泛使用的一種數(shù)據(jù)類型。 運(yùn)用指針編程是C語言最主要的風(fēng)格之一。利用指針變量可以表示各種數(shù)據(jù)結(jié)構(gòu); ...
    朱森閱讀 3,618評論 3 44
  • 數(shù)組在程序設(shè)計中,為了處理方便, 把具有相同類型的若干變量按有序的形式組織起來。這些按序排列的同類數(shù)據(jù)元素的集合稱...
    朱森閱讀 4,279評論 2 13
  • matlab命令 聲明:本文轉(zhuǎn)自:https://www.douban.com/note/136332003/ 侵...
    我就是個初學(xué)者閱讀 14,532評論 0 44
  • 數(shù)據(jù)結(jié)構(gòu)的學(xué)習(xí) 基礎(chǔ)知識 R的賦值符號不是=,而是<- Console控制臺=Linux命令行 R的代碼都是帶括號...
    Hannahhao閱讀 265評論 0 0

友情鏈接更多精彩內(nèi)容