R 小知識積累

2022-07-22 06:15:14 字數 3648 閱讀 5060

1、grep

1

## a為乙個data frame,取含有rna-seq的行

2 index <- grep("

rna-seq

", a$assay_type)

3 b <- a[index,]

2、讀取檔案,選擇不讀哪一行

1

##不讀取帶有!的行

2 data <- read.table("

file

",comment.char="

!", sep="

\t")

3、去掉資料框的某一列,新增行名, 列名

#

#刪除第一列

b<-b[,-1]

##新增行名,列名

rowname(b) <- b[,1]

colname(b)

<- b[1,]

4、sort

1

##a為一data frame2#

#最小值

3 sort(a$num)[1] ; 等價min(a$num)4#

#四分位

5 sort(a$num, decreasing=t)[1] ; 等價max(a$num)6#

#five number, 最小值,下四分位,中位數,上四分位,最大值

7fivenum(a$num)8#

#<5000的行

9 b <- a[a$num < 5000,]

5、strsplit 切割

1

##以2進行分割

2 b <- "

adfdfd2jdfkd

"3 strsplit(b, "2"

)4 adfdf, jdfkd

6、t檢驗

1 t.test(1,2) 檢視p value,若<0.05,則顯著

7、paste, rep

1

##a, 連續貼上4次

2 paste("

a",1:5)

3結果:

4 a 1, a 2, a 3, a 45#

#a, 連續貼上4次,以「-」連線

6 paste("

a",1:5, sep='-')

7結果:

8 a-1, a-2, a-3, a-4910

##複製a 4次

11 rep("

a", 4)

12結果:

13 a,a,a,a

1

##b 為乙個data frame, 每一行求平均數

3mean(x)4})

5 其中 1 代表行,2代表列

6上面可以簡寫為:

9、輸出

1 ## 比如輸出pdf

2 pdf("test.pdf", width=12, height=10)

3 ggplot(data, aes(x,y)) +geom_line()

4 dev.off

10、quantile()

## 在r語言中取百分位比用quantile()函式

> data

<

- c(1,2,3,4,5,6,7,8,9,10)

>

quantile(data,0.5)

50%

5.5

> quantile(data,c(0.25,0.75))

25% 75%

3.25 7.75

## 可以畫閾值線,比如bsa畫圖時;有index 資料 data

p + geom_hline(yintercept =quantile(data, 0.95) ,color="blue",linetype=2,size-0.5)

11、which()

1

## which 可以篩選資料庫中特有的行,比如有資料框data,篩選出delt大於0的行

2 zheng <

- data[which(data$delt

>0),]

12、melt(),dcast()

1

## melt() 和 dcast()為reshape2中的命令23

## melt(): 可以將長的資料框變為短的

4> head(new_data)

5pos xx_mean xy_mean yy_mean

619 36001 1.4052965 0.8884643 1.0787320

720 38001 0.8538059 1.0241777 1.1718369

821 40001 1.7150511 1.5263786 1.4663535

923 44001 0.0000000 1.8001673 0.9257189

1024 46001 0.2837331 1.5285861 1.5567648

1125 48001 0.6670272 2.0511266 0.0000000

1213

> head(melt(new_data,id.vars = "pos"))

14pos variable value

151 36001 xx_mean 1.4052965

162 38001 xx_mean 0.8538059

173 40001 xx_mean 1.7150511

184 44001 xx_mean 0.0000000

195 46001 xx_mean 0.2837331

206 48001 xx_mean 0.6670272

2122

以「pos」為不變的id列,其它變數合併在variable列中,其對應的值為value列

2324

## dcast(): 可以將短的資料框變為長的

25> head(test)

26pos variable value

271 36001 xx_mean 1.4052965

282 38001 xx_mean 0.8538059

293 40001 xx_mean 1.7150511

304 44001 xx_mean 0.0000000

315 46001 xx_mean 0.2837331

326 48001 xx_mean 0.6670272

3334

> head(dcast(test,pos~variable))

35pos xx_mean xy_mean yy_mean

361 36001 1.4052965 0.8884643 1.0787320

372 38001 0.8538059 1.0241777 1.1718369

383 40001 1.7150511 1.5263786 1.4663535

394 44001 0.0000000 1.8001673 0.9257189

405 46001 0.2837331 1.5285861 1.5567648

416 48001 0.6670272 2.0511266 0.0000000

4243 ~左邊的表示不變的列,右邊則展開

---end----

小知識積累

1.有關 和printf的應用 printf是從右向左壓棧 printf d,d n ptr,ptr 先是 ptr 從右至左,所以輸出的兩個值是一樣的 ptr 123 那麼先是 ptr 123 然後是ptr 2.float a 1.0f int a的作用是將浮點數字址開始的sizeof int 個位...

jsp小知識積累

清除頁面快取 response.setheader pragma no cache response.setheader cache control no cache response.setdateheader expires 0 getoutputstream has already been ...

前端小知識積累

1.常用隱藏編輯框的方法 方法1 nf cus this.blur 方法2 readonly 方法3 disabled 通過控制屬性 id attr 控制是否能編輯 特例 特殊編輯文字 如下拉列表 不可使用以上控制 不可逆的 處理方式 給標籤新增乙個容器,通過控制容器的hide和show實現 如 d...