R-note

Posted jszd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R-note相关的知识,希望对你有一定的参考价值。

*一些操作:

运行R脚本:source (“Rscript”)

当前目录下的文件:dir()

*画图

########R语言画图

###RMSD

getwd()

file <- read.csv("backbone_rmsd.csv",sep=‘‘,header = FALSE)

options(max.print = 5000)

file

y <- file[,1]

x <- c(1:length(y))

#plot(x,y,type="l",main="RMSD",xlab = "time",ylab = "rmsd",col = ‘red‘,ylim=c(1,4))

 

file2 <- read.table("bcl_rmsd.dat",sep=‘‘,header = FALSE)

file2

y2 <- file2[,1]

x2 <- c(1:length(y))

 

file3 <- read.table("rg1_rmsd.dat",sep=‘‘,header = FALSE)

file3

y3 <- file3[,1]

x3 <- c(1:length(y))

 

plot(y,type="l",main="RMSD",xlab = "time",ylab = "rmsd",col = ‘red‘,ylim=c(1,4))

lines(y2,type="l",main="RMSD",xlab = "time",ylab = "rmsd",col = ‘green‘,ylim=c(1,4))

lines(y3,type="l",main="RMSD",xlab = "time",ylab = "rmsd",col = ‘black‘,ylim=c(1,4))

 

###测试数据

x <- 1:10

y <- 31:40

m<- matrix(1:100,10,10)    

z<-5:14

plot(x)

bg="red"

par(bg="gray",lty=2,font=4,ps=18)

plot(x,y,axes = TRUE,sub = "sd",main = "dfsdf",type="l")

text(2,32,"text")

pie(x)

boxplot(x,y,z)

hist(x)

barplot(x)

points(1,2)

lines(c(1:5))

*传课初学:

getwd()

a <- 4

print (a)

class(a)

#####matrix矩阵创建

mat <- matrix(c(1:12),nrow=3,ncol=4)

mat

dim(mat)

b = mat*3

b

mat2 <- matrix(c(1:16),nrow=4)

mat2

c = mat %*% mat2

c

colnames(mat) <- c(‘语文‘,‘数学‘,‘英语‘,‘体育‘)

rownames(mat) <- c(‘小明‘,‘小花‘,‘小刚‘)

mat

#矩阵选择

mat[1,2]

mat[2,]

mat[,3]

mat[c(2,3),c(1,2)]

mat[c(2,3),c(3,4)]

#矩阵筛选

mat[mat[,2] >= 5,]

which(mat[,4] >= 11)

mat[which(mat[,4] >= 11),]

#矩阵操作apply

mean(mat[,2])

apply(mat,1,mean)

apply(mat,2,mean)

apply(mat,2,sum)

 

###逻辑运算,&,|,略

a <- c(3,2,2)

all(a == 2)

any(a == 2)

3 %in% a

which(a == 2)

 

###for循环

a <- c(‘a‘,‘b‘,‘c‘)

for (i in c(1:length(a))) print (i)

for (i in c(1:length(a))) print (a[i])

for (i in a) print (i)

a <- matrix(c(1:16),nrow=4)

print(a)

for (i in c(1:nrow(a))) for (j in c(1:ncol(a)))

  print(a[i,j])

 

###repeat循环

###while循环

s <- 0

i <- 1

while(i <= 100)

  s <- s + i

  i <- i + 1

s

 

###自定义函数

printtest <- function(x)print(x)

printtest("hello,world!")

 

ret <- function(x,y)return(x+y)

ret(2,4)

 

###文本读取

#设置当前操作路径

getwd()

setwd("D:\\Rstudio-workplace\\home1")

setwd("D:/Rstudio-workplace/home1")

#!!!处理的文件最后一行应该是空行

dat <- read.table("1.txt",sep=‘ ‘,header = TRUE)

dat

write.table(dat,"11.txt",sep=‘ ‘)

write.table(dat,"111.txt",sep=‘,‘,row.names = FALSE,col.names = FALSE)

#读写csv文件

a = read.csv("1.csv",header = FALSE)

dim(a)

 

###画图

#条形图

x <- c(1:3)

y <- x+2.5

plot(x,y)

plot(x,y,type = "l")

#example

dim(cars)

plot(cars,type = "o",main = ‘测试图‘,xlab="速度",ylab = "dist")

#条形图

barplot(c(88,79,99),col=rainbow(3),names.arg = c(‘小明‘,"小红","小刚"),ylim=c(0,100),legend.text = c(‘小明‘,"小红","小刚"))

#频率分布直方图

hist(iris[,1],col = rainbow(8))

#饼图

pie(c(1,2,3),labels= c("中国","美国","日本"),radius = 0.7)

#箱图(盒图)

boxplot(iris[,c(1:4)],notch = TRUE,col = rainbow(4),names=c("a","b","c","d"))

#图形组合

par(mfrow=c(2,2))

plot(c(1:3))

pie(c(1:3))

boxplot(iris[,c(1:4)],notch = TRUE,col = rainbow(4),names=c("a","b","c","d"))

plot(cars,type = "o",main = ‘测试图‘,xlab="速度",ylab = "dist")

par(mfrow=c(1,1))

 

###线性回归

s1 <- scale(iris[,1])

pw <- scale(iris[,4])

model <- lm(pw~s1)

summary(model)

model <- lm(pw~s1-1)

summary(model)

plot(pw,s1,col=‘green‘)

 

###主成分分析

sc <- scale(swiss)

#构建模型

pri <- princomp(sc,cor=TRUE)

#确定主成分个数

screeplot(pri,type=‘line‘)

#计算每一个城市在每一个主成分上的得分

pre <- predict(pri)

summary(pre)

pre

#特征值

y <- eigen(cor(sc))

y$values

#计算最终得分

scores <- (y$values[1]*pre[,1]+y$values[2]*pre[,2]+y$values[3]*pre[,3]+y$values[4]*pre[,4])/sum(y$values[1:4])

scores

plot(scores)

以上是关于R-note的主要内容,如果未能解决你的问题,请参考以下文章