关于R语言的问题

Python020

关于R语言的问题,第1张

这是我以前学习C语言时做过的,希望对你有所帮助!

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

int shoudsave=0/* */

struct student

{

char num[10]/* 学号 */

char name[20]

char sex[4]

int cgrade

int mgrade

int egrade

int totle

int ave

char neartime[10]/* 最近更新时间 */

}

typedef struct node

{

struct student data

struct node *next

}Node,*Link

void menu()

{

printf("********************************************************************************")

printf("\t1登记学生资料\t\t\t\t\t2删除学生资料\n")

printf("\t3查询学生资料\t\t\t\t\t4修改学生资料\n")

printf("\t5保存学生资料\t\t\t\t\t0退出系统\n")

printf("********************************************************************************\n")

}

void printstart()

{

printf("-----------------------------------------------------------------------\n")

}

void Wrong()

{

printf("\n=====>提示:输入错误!\n")

}

void Nofind()

{

printf("\n=====>提示:没有找到该学生!\n")

}

void printc() /* 本函数用于输出中文 */

{

printf(" 学号\t 姓名 性别 英语成绩 数学成绩 C语言成绩 总分 平均分\n")

}

void printe(Node *p)/* 本函数用于输出英文 */

{

printf("%-12s%s\t%s\t%d\t%d\t%d\t %d\t %d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave)

}

Node* Locate(Link l,char findmess[],char nameornum[]) /* 该函数用于定位连表中符合要求的接点,并返回该指针 */

{

Node *r

if(strcmp(nameornum,"num")==0) /* 按学号查询 */

{

r=l->next

while(r!=NULL)

{

if(strcmp(r->data.num,findmess)==0)

return r

r=r->next

}

}

else if(strcmp(nameornum,"name")==0) /* 按姓名查询 */

{

r=l->next

while(r!=NULL)

{

if(strcmp(r->data.name,findmess)==0)

return r

r=r->next

}

}

return 0

}

void Add(Link l) /* 增加学生 */

{

Node *p,*r,*s

char num[10]

r=l

s=l->next

while(r->next!=NULL)

r=r->next/* 将指针置于最末尾 */

while(1)

{

printf("请你输入学号(以'0'返回上一级菜单:)")

scanf("%s",num)

if(strcmp(num,"0")==0)

break

while(s)

{

if(strcmp(s->data.num,num)==0)

{

printf("=====>提示:学号为'%s'的学生已经存在,若要修改请你选择'4 修改'!\n",num)

printstart()

printc()

printe(s)

printstart()

printf("\n")

return

}

s=s->next

}

p=(Node *)malloc(sizeof(Node))

strcpy(p->data.num,num)

printf("请你输入姓名:")

scanf("%s",p->data.name)

getchar()

printf("请你输入性别:")

scanf("%s",p->data.sex)

getchar()

printf("请你输入c语言成绩:")

scanf("%d",&p->data.cgrade)

getchar()

printf("请你输入数学成绩:")

scanf("%d",&p->data.mgrade)

getchar()

printf("请你输入英语成绩:")

scanf("%d",&p->data.egrade)

getchar()

p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade

p->data.ave=p->data.totle / 3

/* 信息输入已经完成 */

p->next=NULL

r->next=p

r=p

shoudsave=1

}

}

void Qur(Link l) /* 查询学生 */

{

int sel

char findmess[20]

Node *p

if(!l->next)

{

printf("\n=====>提示:没有资料可以查询!\n")

return

}

printf("\n=====>1按学号查找\n=====>2按姓名查找\n")

scanf("%d",&sel)

if(sel==1)/* 学号 */

{

printf("请你输入要查找的学号:")

scanf("%s",findmess)

p=Locate(l,findmess,"num")

if(p)

{

printf("\t\t\t\t查找结果\n")

printstart()

printc()

printe(p)

printstart()

}

else

Nofind()

}

else if(sel==2) /* 姓名 */

{

printf("请你输入要查找的姓名:")

scanf("%s",findmess)

p=Locate(l,findmess,"name")

if(p)

{

printf("\t\t\t\t查找结果\n")

printstart()

printc()

printe(p)

printstart()

}

else

Nofind()

}

else

Wrong()

}

void Del(Link l) /* 删除 */

{

int sel

Node *p,*r

char findmess[20]

if(!l->next)

{

printf("\n=====>提示:没有资料可以删除!\n")

return

}

printf("\n=====>1按学号删除\n=====>2按姓名删除\n")

scanf("%d",&sel)

if(sel==1)

{

printf("请你输入要删除的学号:")

scanf("%s",findmess)

p=Locate(l,findmess,"num")

if(p)

{

r=l

while(r->next!=p)

r=r->next

r->next=p->next

free(p)

printf("\n=====>提示:该学生已经成功删除!\n")

shoudsave=1

}

else

Nofind()

}

else if(sel==2)

{

printf("请你输入要删除的姓名:")

scanf("%s",findmess)

p=Locate(l,findmess,"name")

if(p)

{

r=l

while(r->next!=p)

r=r->next

r->next=p->next

free(p)

printf("\n=====>提示:该学生已经成功删除!\n")

shoudsave=1

}

else

Nofind()

}

else

Wrong()

}

void Modify(Link l)

{

Node *p

char findmess[20]

if(!l->next)

{

printf("\n=====>提示:没有资料可以修改!\n")

return

}

printf("请你输入要修改的学生学号:")

scanf("%s",findmess)

p=Locate(l,findmess,"num")

if(p)

{

printf("请你输入新学号(原来是%s):",p->data.num)

scanf("%s",p->data.num)

printf("请你输入新姓名(原来是%s):",p->data.name)

scanf("%s",p->data.name)

getchar()

printf("请你输入新性别(原来是%s):",p->data.sex)

scanf("%s",p->data.sex)

printf("请你输入新的c语言成绩(原来是%d分):",p->data.cgrade)

scanf("%d",&p->data.cgrade)

getchar()

printf("请你输入新的数学成绩(原来是%d分):",p->data.mgrade)

scanf("%d",&p->data.mgrade)

getchar()

printf("请你输入新的英语成绩(原来是%d分):",p->data.egrade)

scanf("%d",&p->data.egrade)

p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade

p->data.ave=p->data.totle/3

printf("\n=====>提示:资料修改成功!\n")

shoudsave=1

}

else

Nofind()

}

void Disp(Link l)

{

int count=0

Node *p

p=l->next

if(!p)

{

printf("\n=====>提示:没有资料可以显示!\n")

return

}

printf("\t\t\t\t显示结果\n")

printstart()

printc()

printf("\n")

while(p)

{

printe(p)

p=p->next

}

printstart()

printf("\n")

}

void Tongji(Link l)

{

Node *pm,*pe,*pc,*pt,*pa/* 用于指向分数最高的接点 */

Node *r=l->next

if(!r)

{

printf("\n=====>提示:没有资料可以统计!\n")

return

}

pm=pe=pc=pt=pa=r

while(r!=NULL)

{

if(r->data.cgrade>=pc->data.cgrade)

pc=r

if(r->data.mgrade>=pm->data.mgrade)

pm=r

if(r->data.egrade>=pe->data.egrade)

pe=r

if(r->data.totle>=pt->data.totle)

pt=r

if(r->data.ave>=pa->data.ave)

pa=r

r=r->next

}

printf("------------------------------统计结果--------------------------------\n")

printf("总分最高者:\t%s %d分\n",pt->data.name,pt->data.totle)

printf("平均分最高者:\t%s %d分\n",pa->data.name,pa->data.ave)

printf("英语最高者:\t%s %d分\n",pe->data.name,pe->data.egrade)

printf("数学最高者:\t%s %d分\n",pm->data.name,pm->data.mgrade)

printf("c语言最高者:\t%s %d分\n",pc->data.name,pc->data.cgrade)

printstart()

}

void Sort(Link l)

{

Link ll

Node *p,*rr,*s

ll=(Link)malloc(sizeof(Node))/* 用于做新的连表 */

ll->next=NULL

if(l->next==NULL)

{

printf("\n=====>提示:没有资料可以排序!\n")

return

}

p=l->next

while(p)

{

s=(Node*)malloc(sizeof(Node))/* 新建接点用于保存信息 */

s->data=p->data

s->next=NULL

rr=ll

while(rr->next!=NULL &&rr->next->data.totle>=p->data.totle)

rr=rr->next

if(rr->next==NULL)

rr->next=s

else

{

s->next=rr->next

rr->next=s

}

p=p->next

}

free(l)

l->next=ll->next

printf("\n=====>提示:排序已经完成!\n")

}

void Save(Link l)

{

FILE* fp

Node *p

int flag=1,count=0

fp=fopen("c:\\student","wb")

if(fp==NULL)

{

printf("\n=====>提示:重新打开文件时发生错误!\n")

exit(1)

}

p=l->next

while(p)

{

if(fwrite(p,sizeof(Node),1,fp)==1)

{

p=p->next

count++

}

else

{

flag=0

break

}

}

if(flag)

{

printf("\n=====>提示:文件保存成功.(有%d条记录已经保存.)\n",count)

shoudsave=0

}

fclose(fp)

}

void main()

{

Link l/* 连表 */

FILE *fp/* 文件指针 */

int sel

char ch

char jian

int count=0

Node *p,*r

printf("\t\t\t\t学生成绩管理系统\n\t\t\t\t\n")

l=(Node*)malloc(sizeof(Node))

l->next=NULL

r=l

fp=fopen("C:\\student","rb")

if(fp==NULL)

{

printf("\n=====>提示:文件还不存在,是否创建?(y/n)\n")

scanf("%c",&jian)

if(jian=='y'||jian=='Y')

fp=fopen("C:\\student","wb")

else

exit(0)

}

printf("\n=====>提示:文件已经打开,正在导入记录......\n")

while(!feof(fp))

{

p=(Node*)malloc(sizeof(Node))

if(fread(p,sizeof(Node),1,fp)) /* 将文件的内容放入接点中 */

{

p->next=NULL

r->next=p

r=p/* 将该接点挂入连中 */

count++

}

}

fclose(fp)/* 关闭文件 */

printf("\n=====>提示:记录导入完毕,共导入%d条记录.\n",count)

while(1)

{

menu()

printf("请你选择操作:")

scanf("%d",&sel)

if(sel==0)

{

if(shoudsave==1)

{ getchar()

printf("\n=====>提示:资料已经改动,是否将改动保存到文件中(y/n)?\n")

scanf("%c",&ch)

if(ch=='y'||ch=='Y')

Save(l)

}

printf("\n=====>提示:你已经退出系统,再见!\n")

break

}

switch(sel)

{

case 1:Add(l)break/* 增加学生 */

case 2:Del(l)break/* 删除学生 */

case 3:Qur(l)break/* 查询学生 */

case 4:Modify(l)break/* 修改学生 */

case 5:Save(l)break/* 保存学生 */

case 9:printf("\t\t\t==========帮助信息==========\n")break

default: Wrong()getchar()break

}

}

}

前言: 在构建ceRNA 网络时,需要计算lncRNA 与 蛋白编码gene (pc gene) 间的表达相关性,一般采用皮尔逊相关系数。具体如何做呢?

2.获得mRNA的表达矩阵

4个基因在100个样本的表达量矩阵:

3.计算lncRNA 与gene 的表达相关性

使用cor()函数进行皮尔森相关系数计算,就是这么简单:

R语言基本数据分析

本文基于R语言进行基本数据统计分析,包括基本作图,线性拟合,逻辑回归,bootstrap采样和Anova方差分析的实现及应用。

不多说,直接上代码,代码中有注释。

1. 基本作图(盒图,qq图)

#basic plot

boxplot(x)

qqplot(x,y)

2. 线性拟合

#linear regression

n = 10

x1 = rnorm(n)#variable 1

x2 = rnorm(n)#variable 2

y = rnorm(n)*3

mod = lm(y~x1+x2)

model.matrix(mod) #erect the matrix of mod

plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance

summary(mod) #get the statistic information of the model

hatvalues(mod) #very important, for abnormal sample detection

3. 逻辑回归

#logistic regression

x <- c(0, 1, 2, 3, 4, 5)

y <- c(0, 9, 21, 47, 60, 63) # the number of successes

n <- 70 #the number of trails

z <- n - y #the number of failures

b <- cbind(y, z) # column bind

fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model

print(fitx)

plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)

beta0 <- fitx$coef[1]

beta1 <- fitx$coef[2]

fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))

par(new=T)

curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve

3. Bootstrap采样

# bootstrap

# Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution

dat = matrix(rnorm(100*5),100,5)

no.samples = 200 #sample 200 times

# theta = matrix(rep(0,no.samples*5),no.samples,5)

theta =rep(0,no.samples*5)

for (i in 1:no.samples)

{

j = sample(1:100,100,replace = TRUE)#get 100 samples each time

datrnd = dat[j,]#select one row each time

lambda = princomp(datrnd)$sdev^2#get eigenvalues

# theta[i,] = lambda

theta[i] = lambda[1]/sum(lambda)#plot the ratio of the biggest eigenvalue

}

# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue

hist(theta)#plot the percentage distribution of the biggest eigenvalue

sd(theta)#standard deviation of theta

#上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能

4. ANOVA方差分析

#Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)

#

y = rnorm(9)#weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入

#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)

Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group

mod = lm(y~Treatment) #linear regression

print(anova(mod))

#解释:Df(degree of freedom)

#Sum Sq: deviance (within groups, and residuals) 总偏差和

# Mean Sq: variance (within groups, and residuals) 平均方差和

# compare the contribution given by Treatment and Residual

#F value: Mean Sq(Treatment)/Mean Sq(Residuals)

#Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)

qqnorm(mod$residual) #plot the residual approximated by mod

#如果qqnorm of residual像一条直线,说明residual符合正态分布,也就是说Treatment带来的contribution很小,也就是说Treatment无法带来收益(多喂维他命少喂维他命没区别)

如下面两图分别是

(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和

(右)y = rnorm(9)

的结果。可见如果给定猪吃维他命2后体重特别突出的数据结果后,qq图种residual不在是一条直线,换句话说residual不再符合正态分布,i.e., 维他命对猪的体重有影响。