R

R

readLine

while (length(line <- readLines(con, n = 1, warn = FALSE)) > 0) {
//line by line to the end of the file
}
a = readLines(“SRR8811960_pass_1.fastq”,n=1)
substr(a, 0, 2) // change substring

1. Basic R

seq(1, 10, by=0.5) //间距0.5
seq(1, 10, length.out = 14) //等分14个数
length(z) //计算长度
c(w , z) //合并两个数列
runif(15, 0, 20) //随机生成15个数,在0~20之间
matrix( c(1:12), nrow = 4, byrow = TRUE) //matrix
cbind(x, y, z)
rbind(x, y, z)
tapply(X FUN) //Apply a function to all elements, output table
cat(hokie, “\n”) # \n stands for newline //cat 可以加到后面,把换行加到hokie后面
abs(pi) //abs value
start.time <- Sys.time() //time counter
stop.time <- Sys.time()
stop.time - start.time
cat(“The value of y is:”, y, “\n”) //可以用cat来输出sprintf的效果
if (k %% 2) //k 能整除2则为true
round(runif(12, -20, 20)) //round 为整数
as.numeric(loop_time) //变成数字
victims <- data.frame(read.csv(file=“c:/Users/Lin/Desktop/CS3654/assaultvictims_trimmed.csv”,header = T))
victims <- read.csv(“C:/Users/Lin/Desktop/CS3654/assaultvictims_trimmed.csv”, header = TRUE)
head(victims, n = 3) //展示开头几行
t(relfreqs) //反转一个matrix or table
A <- matrix( rep(1:7, c(1,2,3,4,5,6,7)), nrow = 4, byrow = TRUE) //matrix
prob4df = cbind(prob4df,“error” =error) //combine a matrix 在加一列,列名字为error,值为error本身

mean = c()
mean = append(mean,i) // 往sequency 上加值

for (i in 1:10){
print(i)
}

plot(density(brain$Horsepower),bty=“n”,xlim=c(0,250),lwd=2,main=“Car-horsepower”,xlab = “horsepower”)
//一条density 线

plot(x, y, type=“l”, col = “red”, xlim = c(-1, 7), ylim = c(-1.25, 1.25) ,
xlab = “this is x”, ylab = “this is y”, main=“This is a title”)

plot(c(range1, range2), c(0,2), type = “n”,xlab = “x”,ylab = “y”)
curve(2/(e^(2x)),add=TRUE) // 加曲线线
rect(i
(1/n),(2e^(-2(length/2+(i-1)length))) , 0+(i-1)(1/n), 0) //加长方形

hist(brain$Tumor, probability = T, xlim = c(0, 500) , ylim = c(0, 0.010),
xlab = “Tumor”, ylab = “relative frequency”, main = “Tumor Weights”,
breaks = 20, col=“yellow”)

hist(brain D i s p l a c e m e n t , b r e a k s = 5 , x l i m = c ( 0 , 500 ) , y l i m = c ( 0 , 0.004 ) , f r e q = F A L S E , m a i n = " C a r − D i s p l a c e m e n t " , l a s = 1 , x l a b = " D i s p l a c e m e n t " , c o l = ′ l i g h t b l u e ′ ) l i n e s ( d e n s i t y ( b r a i n Displacement,breaks=5,xlim=c(0,500), ylim=c(0,0.004), freq=FALSE, main="Car-Displacement", las=1, xlab = "Displacement",col='lightblue') lines(density(brain Displacement,breaks=5,xlim=c(0,500),ylim=c(0,0.004),freq=FALSE,main="CarDisplacement",las=1,xlab="Displacement",col=lightblue)lines(density(brainDisplacement, adjust=2),col=“blue”)

boxplot( brain T u m o r   b r a i n Tumor ~ brain Tumor brainSex,
xlab = “Sex”, ylab = “Tumor Weight”, main=“Tumor Weight by Rat Sex”)

boxplot(a,b,c,main=“weight by race”,las=1,ylab =“weight range”,xlab=“race”,
col=c(“pink”,“blue”,“cyan”), names=(c( “race 1”,“race 2”,“race 3”)))

stripchart(bwt~race, vertical = TRUE, data = birthwt,
method = “jitter”, add = TRUE, pch = 20, col = ‘blue’)

barplot( table( brain$Sex ) ,
ylim = c(0, 30), col=c(“pink”,“blue”),
main = “Number of Rats of each Sex in the Study” )

barplot(t(relfreqs), main = “R”,
xlab = “Neighborhood Type”, ylab = “Relative Frequency”, col = c(“cyan”,“orange”), las = 1,
legend.text = c(“No Police Reported”, “Police Reported”),
args.legend = list(x = “bottom”, inset = 0.05, bg = “white”)
)

barplot( num ,xlab = “Toxic and Non-Toxic”,
ylab = “Numbers”,
ylim = c(0, 1),las=1,names.arg=(c( “Non-Toxic”,“Toxic”)),
col = c(“pink” , “blue”),
main = “Puso Toxic and Non-Toxic” )

a = table(Data1)
barplot( a)

scatter plots:
plot(subset$Horsepower, main=“Scatterplot”, xlab= “Hoursepower”, ylab = “range”,
las=1,xlim=c(1,400),ylim = c(0,250),col=2)

legend(1, 0.0006, legend=c(“race 1”, “race 2”,“race 3”),
col=c(“blue”, “red”,“cyan”), lty=1:2, cex=0.8)

legend(“topright”, legend=c(“Sophia”, “Mary”),
col=c( “red”,“blue”),pch = c(25,15), cex=0.5, x=…, y=…)
//point legend
//cex size of the legend

corrgram(file, order=TRUE, lower.panel=panel.shade,
upper.panel=panel.pie, diag.panel=panel.minmax,text.panel=panel.txt,
main=“Information on infant birth”)


2. ggplot

Point:
geom_point()
geom_lines()

ggplot(data)+
aes(x = age, y=year) +
geom_point( colour = ‘blue’, alpha = 0.2) //加在geom中为静态

ggplot(data)+
aes(x = age, y=year,colour = gender) + //加在aes中为动态,取决于gender
geom_point( colour = ‘blue’, alpha = 0.2)

Histogram:
ggplot(data)+
aes(x = age) +
geom_bar()

Bar:
data = data %>% mutate(name_New colume = cur(colume_want_to_use, breaks = c(-1,0,5,10,100))) //切数据

ggplot(data)+
aes(x = age,fill = gender) + // (age 为X轴, bar 为gender)statics
geom_bar()

ggplot(data)+
aes(x = age,fill = gender) + //
geom_bar(position = ‘dodge’) //这个会把stack的bar 分成两个独立的bar

ggplot(data)+
aes(x = age,fill = gender) + //
geom_bar(position = ‘fill’) //现实每个分段占据单个bar的百分比

ggplot(victims) + theme_bw() +
geom_bar(mapping = aes(x = MSA, fill = Police), position = “fill”) +
labs(x = “N”, y = “G”,
title = “R”) +
scale_fill_manual(values = c(“cyan”, “orange”))

数据filter
data = data%>%
filter(genter != ‘UNKNOWN’) %>%
filter(age != ‘UNKNOWN’) %>%
group_by(gender,age) %>%
summarise(average = mean(level)) %>%
ungroup()

加一个新的数列到data上面
data $ name = data $ olddata %>% round(1) %>% as.character

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值