趣天学塘 QTXT

随性、随时、随心
点点滴滴的生活记录
在寻找诗和远方的路上
一手机、一相机、一电脑、一本书、一个人、一世界、一生
@ since 2012
Attitude Determines Altitude!

© 趣天学塘 QTXT | Powered by LOFTER

Hello Code 012-合并两个包含同样行名的data.frame

合并两个包含同样行名(rowname相同或者某一列包含相同内容)的数据集

直接上code,折腾了快一下午

library(readr)

library(tidyverse)

dfov <- read_csv("Downloads/xxx/OEC GTEx TCGA/Ovary_exp_normalpluscancer_all_withID.csv")

hippogene <- read_csv("Downloads/xxx/hippogene.csv")

dfov <- data.frame(dfov)

#...

Hello Code 011-colnames rownames 人工输入修改

如何添加c1~c4?r1~r5?

coln = c(”c1”,”c2”,”c3”,”c4”)

rown = c(”r1”,”r2”,”r3”,”r4”,”r5”)

colnames(datamatrix) = coln

rownames(datamatrix) = rown

https://blog.csdn.net/weixin_33922670/article/details/85903958

Hello Code 010-替换data.matrix的colnames为一数据列

ghirow2 <- fread("~/Downloads/ghirow2.csv")

##打开准备用于替换colnames的数据列

cnames <- as.character(ghirow2$V2)

##转换数据列(单列)为as.character! 这是关键

colnames(exp_brain) <- cnames

##转换成功

以上为自己实践揣摩出来的,享受代码之美!

Hello Code 009-subset()数据的查询筛选

subset(x, subset, select, drop = FALSE, ...)

&多条件查询

select 显示指定列

selectresult=subset(df1,name=="aa" & sex=="f",select=c(age,sex))

负号排除指定列

selectresult=subset(df1,name=="aa" & sex=="f",select=c(-age,-sex))

Hello Code 008- 字符串截取(substr()函数和substring()函数)

代码: 参数 备注

substr(s, first, last) :first和last为截取的起始位置。必须设置参数first和last,否则出错。

substring(s, first, last = 1000000): first和last为截取的起始位置。可以只设置first参数,last参数缺省时则默认为1000000,指字符串的最大长度。

Hello Code 007-去除值(NA)

data

data<-na.omit(data)

##去除所有含NA的行

data<-subset(data,value1!="NA")

##去除value1为这一列中含有NA的行

Hello Code 006-转换为数值型数据

> tmp1=as.data.frame(lapply(tmp1,as.numeric))

Hello Code 005-R语言 电脑内存不足的尴尬

错误: The size of the connection buffer (131072) was not large enough

to fit a complete line:

  * Increase it by setting `Sys.setenv("VROOM_CONNECTION_SIZE")`

解决办法:

Sys.setenv("VROOM_CONNECTION_SIZE"=131072*6)

Hello Code 004-mutate新建列之加减乘除

>df_train <- mutate(df, df_train_sixgenes$scores = df_train_sixgenes$PMCH*( -0.6294544) + df_train_sixgenes$IFNB1*( -0.3168858) + df_train_sixgenes$IGF2*( 0.1835164) + df_train_sixgenes$PYCR1*( -0.3942564))

>df_train_sixgenes$scores <- df_train_sixgenes...

Hello Code 003-生存分析 KMplot(perfect)

> library(survminer)

> fit <- survfit(Surv(futime, fustatus)~scores_by2,data = dtrain_score)

> p <- ggsurvplot(fit, conf.int=F, pval=T, risk.table=T, legend.labs = c('High-Risk','Low-Risk'), 

  legend.title='Risk Score', palette = c("dodgerblue2","orchid2...

Hello Code 002-优雅的分成高低表达组

> dtrain_score$scores_by2 <- ifelse(dtrain_score$scores > median(dtrain_score$scores),'High-Risk','Low-Risk')

> dtest_score$scores_by2 <- ifelse(dtest_score$scores > median(dtest_score$scores),'High-Risk','Low-Risk')

Hello Code 001-按列提取数据

> tmp1 <- df_train[c(1:7)]

> tmp2 <- df_train[c(colnames = 'PMCH',colnames = 'IFNB1',colnames = 'IGF2',colnames = 'PYCR1',colnames = 'FOXO1',colnames = 'PPARG')]

> tmp <- cbind(tmp1,tmp2

以上代码需要在as.data.frame中操作,matrix...

1 / 9