其他分享
首页 > 其他分享> > R语言练习(自存)

R语言练习(自存)

作者:互联网

第一次

#1.1.1
x <- seq(from=1,to=99,by=2)

#1.1.2
x<-1:100
index = rep(c(TRUE, FALSE), 100)
x<-x[index]
x<-x[!is.na(x)]

#1.1.3
x<-NULL
num<-1
for(i in 1:50){
  x[i]<-num
 num<-num+2
}

# 1.2 
y<-rep(1:5,10)

# 1.3 
z <- c(x,y)
is.vector(x)
is.vector(y)
is.vector(z)

# 1.4 
x <- x[-1]

# 1.5 
#1.5.1
y <- y[-which(y==1)]

#1.5.2
y<-y[!(y==1)]

# 1.6 
vector<-c(x[2],x[which(x>90)])

# 1.7 
x_odd<- x[seq(from=1,to=length(x),by=2)]
x_even<-x[seq(from=2,to=length(x),by=2)]

# 2.1 
m<-c("Tony","2020110","Male","1998.8.1")

# 2.2 
n<-paste("NJU16",1:30,sep="-")

# 2.3 
n[grep("8",n)]
 


# 2.4 
# 2.5 
statement<-"I Like You"
verb<-substr(statement,3,6)

# 2.6 
substr(statement,3,6) <- "Love"
statement

# 2.7 
strsplit("Jack,Male,35,worker",split=",")

# 2.8 
substr("+86-021-88681188Ext1",5,16)
 

 

# 3.1 
mpg<- data.frame(min(mtcars$mpg),max(mtcars$mpg),mean(mtcars$mpg),sd(mtcars$mpg),median(mtcars$mpg),quantile(mtcars$mpg,0.25),quantile(mtcars$mpg,0.75))

# 3.2 
hp_max <- which.max(mtcars$hp)
hp_max <- which(mtcars$hp==max(mtcars$hp))
hp_min<- which.min(mtcars$hp)
hp_min<- which(mtcars$hp==min(mtcars$hp))

# 3.3 
unique(mtcars$cyl)
table(mtcars$cyl)

# 3.4 
sample <- mtcars[which(mtcars$hp>mean(mtcars$hp)),]

 

 setwd("")
data <- read.csv("data.csv",header = TRUE)
save(read.data,file="read.Rdata")

 

第二次

# 1.1
Student <- c("John Davis", "Angela Williams", "Bullwinkle Moose","David Jones", "Janice Markhammer", "Cheryl Cushing","Reuven Ytzrhak", "Greg Knox", "Joel England","Mary Rayburn")
Math <- c(502, 600, 412, 358, 495, 512, 410, 625, 573, 522)
Science <- c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86)
English <- c(25, 22, 18, 15, 20, 28, 15, 30, 27, 18)
roster <- data.frame(Student, Math, Science, English,stringsAsFactors=FALSE)

roster <- roster[order(roster$Student),]

# 1
Z_new <- scale(roster[,c("Math", "Science", "English")],center = T,scale = T)
roster$score <- apply(Z_new,1,mean)
#apply(X, MARGIN, FUN, …)
#X表示一个数组[一个矩阵]
#MARGIN: 1表示行,2表示列
#FUN表示一种遍历准则,比如连接函数等等
#Apply函数的介绍见:https://www.zhihu.com/question/39843392

Y_Cut <- quantile(roster$score,c(0,0.25,0.50,0.75,1))
roster$Rank <- cut(roster$score,breaks = Y_Cut ,include.lowest=T,labels = c('D','C','B','A')) 
#cut:将数字转换成因子;include.lowest表示是否包括最低

# table(roster$Rank)
 

 

# 2.1
For_ <- function(n){
  q <- 1
  for(i in 1:n){
    q<- q*i
  }
  return(q)
}
# For_(2)

while_ <- function(n){
  q<-1
  i = 1
  while(n>=i){
    q<- q*i
    i <- i+1
  }
  return(q)
}
# while_(3)

# 2.2

HF <- function(x,n){
  q <-1
  for(i in 1:n){
    q <- q +x^n
  }
  return(q)
}
# HF(1,1)  # HF(1,2)

# 2.3

fbo <- function(N){
  if(N==1|N==2){
    return(1)
  }else{
    return(fbo(N-1) + fbo(N-2))
  }
}
# for(i in 1:10) {
#   print(fbo(i))
# }

# 3

set.seed(123465)
my_data <- data.frame(matrix(sample.int(100,1000,replace = T),100,10))
names(my_data) <- paste0('评委',1:10)
my_data$ID <- 1:100


Score <- function(x){
  max_ <- which.max(x)[1]
  min_ <- which.min(x)[1]
  x <- x[-c(max_,min_)]
  return(mean(x,na.rm = T))
}
my_data$score <- apply(my_data[,paste0('评委',1:10)],1,Score)



my_data$score <- 0
for(i in 1:nrow(my_data)){
  my_data$score[i] <- Score(as.numeric(my_data[i,paste0('评委',1:10)]))
}


################################################


set.seed(123465)
my_data <- data.frame(matrix(sample.int(100,100000,replace = T),10000,10))
names(my_data) <- paste0('评委',1:10)
my_data$ID <- 1:100

a <- Sys.time()
my_data$score <- apply(my_data[,paste0('评委',1:10)],1,Score)

(b <-   Sys.time() -a)

a <- Sys.time()
my_data$score <- 0
for(i in 1:nrow(my_data)){
  my_data$score[i] <- Score(as.numeric(my_data[i,paste0('评委',1:10)]))
}
(b <-   Sys.time() -a)
###########################################################

library(parallel)
cores <- detectCores()
cl <- makeCluster(cores-2)
Res <- parApply(cl = cl, my_data[,paste0('评委',1:10)],1,Score)

stopCluster(cl)
 

 第三次

# 1.

mean_res <- aggregate(mtcars,list(mtcars$cyl),mean)
boxplot(mpg ~ cyl, data=mtcars,
        main="Car Mileage Data",
        xlab="Number of Cylinders",
        ylab="Miles Per Gallon",
        col = c('red','blue','green'))
points(2,c(mean_res$mpg[2]+5),col='red',pch = 8)
lines(1:3,mean_res$mpg,col = grey(0.5),lwd=2,lty = 6)

points(1:3,c(mean_res$mpg[1:3]),col = grey(0.5),lwd=2,pch = 14)
#points(1:3,mean_res$mpg,col = grey(0.5),lwd=2,pch = 14)

# 2.
x <- c(1:10)
y <- x
z <- 10/x
#https://zhuanlan.zhihu.com/p/29624736
opar <- par(no.readonly=TRUE)
par(pin=c(4,3))
plot(x, y, type="b",
     pch=21, col="red",
     yaxt="n", lty=3, ann=FALSE)
lines(x, z, type="b", pch=22, col="blue", lty=2)
axis(2, at=x, labels=x, col.axis="red", las=2)
axis(4, at=z, labels=round(z, digits=2),
     col.axis="blue",   cex.axis=0.7, tck=-.01)
mtext("y=10/x", side=4, line=3, cex.lab=1,  col="blue")
title("An Example of Creative Axes",
      xlab="X values",
      ylab="Y=X")
par(opar)
 

 

# 3.

opar <- par(no.readonly=TRUE)
par(mfrow=c(2, 2),cex.lab = 1.5,mar = c(6,6,2,2)) 

plot(mtcars$wt,mtcars$mpg,xlab = 'wt',ylab = 'mpg')  ##  
plot(factor(mtcars$cyl),mtcars$mpg,xlab = 'cyl',ylab = 'mpg')  
## boxplot(mpg~cyl,data=mtcars)
plot(factor(mtcars$cyl),factor(mtcars$gear),
     xlab = 'gear',ylab = 'cyl')   
## 
# count_ <- table(mtcars$gear,mtcars$cyl)
# barplot(count_,axes=F,xlab = 'gear',ylab = 'cyl') 
count_ <- table( mtcars$cyl)
pie(count_, labels = paste0("cyl_",names(count_)),radius = 1.2)
par(opar)

# 4.
#pdf("mygraph.pdf",width = 7,height = 5) 
  opar <- par(no.readonly=TRUE)
  par(fig=c(0,0.8,0,0.8))
  plot(mtcars$wt, mtcars$mpg,xlab="Miles Per Gallon",ylab="Car Weight")
  par(fig=c(0, 0.8, 0.35,1), new=TRUE)
  boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE ,col="red")
  par(fig=c(0.55, 0.99, 0, 0.8), new=TRUE)
  boxplot(mtcars$mpg, axes=FALSE,col="blue")
  par(opar)
#dev.off()
 

 第四次

# 1. 
library(ggplot2)
mtcars$cyl <- factor(mtcars$cyl)
ggplot(mtcars, aes(x=cyl, y=mpg)) +
  geom_boxplot( color="black", 
               notch=TRUE,
               fill = c('red','blue','green'))+
  labs(x ="Number of Cylinders" ,y="Miles Per Gallon")+
  geom_point(position="jitter", color="blue", alpha=.5)

# 2.
ggplot(mtcars, aes(x=wt, y=mpg, size=disp)) +
  geom_point(shape=21, color="black", fill="cornsilk") +
  labs(x="Weight", y="Miles Per Gallon", size="Engine\nDisplacement")

 

# 3.
  library(gridExtra)
  p1 <- ggplot(data =mtcars ) + geom_point(aes(x=wt,y=mpg))
  p2 <- ggplot(data =mtcars ) + geom_bar(aes(x= am,fill = cyl),position = 'fill')
  grid.arrange(p1,p2, ncol=2)

#4
library(ggplot2)
 mytheme <- theme(plot.title=element_text(face="bold.italic",
                                           size="14", color="brown"),
                   axis.title=element_text(face="bold.italic",
                                           size=10, color="brown"),
                   axis.text=element_text(face="bold", size=9,
                                          color="darkblue"),
                   panel.background=element_rect(fill="white",
                                                 color="darkblue"),
                   panel.grid.major.y=element_line(color="grey",
                                                   linetype=1),
                   panel.grid.minor.y=element_line(color="grey",
                                                   linetype=2),
                   panel.grid.minor.x=element_blank(),
                   legend.position="top")

 第五次

# 6.1
data1 <- c(27, 18, 15, 24, 36, 30)
ks.test(data1,y = 'runif')

# 6.2
data2 <- c(36,36,37,38,40,42,43,43,44,45,48,48,50,50,51,
           52,53,54,54,56,57,57,57,58,58,58,58,58,59,60,
           61,61,61,62,62,63,63,65,66,68,68,70,73,73,75)
shapiro.test(data2) 

 #6.3
data3 <- data.frame(nrow = c(1,1,1,2,2,2),
                    ncol = c(1,2,3,1,2,3),
                    freq = c(341,405,105,103,11,15))
mid_tab = xtabs(freq~nrow+ncol,data = data3)
mid_table<-matrix(c(341,405,105,103,11,15),nrow=2,ncol=3)
chisq.test(mid_tab)
chisq.test(mid_table)

 

# 6.4
X<-c(25.6, 22.2, 28.0, 29.8, 24.4, 30.0, 29.0, 27.5, 25.0, 27.7,
       23.0, 32.2, 28.8, 28.0, 31.5, 25.9, 20.6, 21.2, 22.0, 21.2)
A<-factor(rep(1:5, each=4))
data4<-data.frame(X, A)
aov.mis<-aov(X~A, data=data4)
summary(aov.mis)

plot(data4$X~data4$A)

# # summary(fit)
# library(gplots)
# plotmeans(X ~ A, data=data4)

plot(TukeyHSD(aov.mis))

pairwise.t.test(X, A, p.adjust.method="none")
pairwise.t.test(X, A, p.adjust.method="bonferroni")

 

# 6.5
group_<-rep(c("A","B","C"),each=8)
Weight_A <- c(15,13,11,12,12,16,14,17,17,16,
                    18,18,21,22,19,18,22,24,20,23,
                    25,27,30,32)
Y <-c(85,83,65,76,80,91,84,90,97,90,
                     100,95,103,106,99,94,89,91,83,
                     95,100,102,105,110)

data5<-data.frame(group_,Weight_A,Y)


res_5 <- aov(Y ~ Weight_A+group_ , data=data5)
summary(res_5)

############################  ... 
my_own <- function(x){
  res <- mean(x)
  return(res)
}
my_own(c(1:100))
my_own(c(1:100,NA,NA,4,5))


my_own2 <- function(x,...){
  res <- mean(x,...)
  return(res)
}
my_own2(c(1:100,NA,NA,4,5),na.rm =T)
 

 

 

 

 

 

 

 

 

标签:mpg,自存,语言,mtcars,my,练习,data,col,cyl
来源: https://blog.csdn.net/m0_64126001/article/details/121845511