R
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 Displacement, adjust=2),col=“blue”)
boxplot( brain
Sex,
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
转载:https://blog.csdn.net/weixin_45703606/article/details/101812568