R相关命令

2021-05-26  本文已影响0人  MR来了
## 设置R的lib路径
.libPaths("C:/Program Files/R/R-3.5.2/library")

## 数据转换
tmp  <-  as.data.frame(tmp)
tc.cancer$RP  <-  as.numeric(tc.cancer$RP)
DCid <-  as.character(DCid)

## 备份文件
write.table(rownames(rpdata.rp.used), file = "rpdata.rp.used", append = FALSE, quote = FALSE, sep = "\n",eol = "\n", na = "NA", dec = ".", row.names = FALSE)

### ensemble id 转 gene symbol
library(Seurat)
library(data.table)
library(DropletUtils)
library(org.Hs.eg.db)

## 后面两个转不出来为NA
inputs <- c('ENSG00000283239', 'ENSG00000273748', 'ENSG00000273748')
ids=select(org.Hs.eg.db,keys = inputs,
    columns = c('ENSEMBL','SYMBOL'),
    keytype = 'ENSEMBL')

## 读取文件一列作为vector
df <- read.table('./ups.txt', header = FALSE)
df <- as.vector(df$V1)

## 建立制定函数的R的dataframe
results <-  as.data.frame(array(,dim=c(length( rownames(PRO[["RNA"]]@data) ),0)))

## 判断某一列是否有某个元素
tcrs_cells = rownames(tcrs)
### 判断是否存在
check_in <- function(x) {
    if( x  %in% tcrs_cells){
        return("TCR")
    }else{
        return("no TCR")
    }
}
PRO1@meta.data$TCR = sapply(PRO1@meta.data$barcode, check_in)


R字符处理相关

## 以空格隔开
tmp <- unlist(strsplit("1 Tumour core",  " "))




R画图相关

## 设置画版大小
options(repr.plot.width=14, repr.plot.height=6)

文件输出

## 输出dataframe的某几列
pairs <- CellChatDB$interaction[c("ligand","receptor")]
write.table(pairs ,file="cellchat.pairs.csv",sep="\t", row.names=FALSE, na = "NA", quote=FALSE)

上一篇下一篇

猜你喜欢

热点阅读