R语言复习
苟亚飞06-22 12:15阅读:1删除编辑
setwd("d:/rdata")
bdt <- read.csv("body.csv")
bdt <- read.csv("body.csv",
encoding = "GBK")
bdt <- read.csv("body.csv",
fileEncoding = "GBK")
bdt <- read.csv("body.csv",
fileEncoding = "GB18030")
bdt
attach(bdt)
shapiro.test(height)
hist(height)
plot(density(height))
spdt <- read.csv("spdata2.csv")
shapiro.test(spdt$body_weight)
hist(spdt$body_weight)
plot(density(spdt$body_weight))
plot(density(spdt$brain_weight))
shapiro.test(log(spdt$body_weight))
hist(log(spdt$body_weight))
height
gender
mean(height[gender=="M"]) -
mean(height[gender=="F"])
t.test(height[gender=="M"],height[gender=="F"])
ob.d =mean(height[gender=="M"]) -
mean(height[gender=="F"])
null.d <- ob.d
for( i in 1:999){
null.h <- sample(height)
null.d <- c(null.d,mean(null.h[1:9]-mean(null.h[10:18])))
}
null.d
rank(null.d)[1]
hist(null.d)
abline(v = ob.d)
u1 = which(rank(null.d,ties.method = 'first')==25)
u2 =which(rank(null.d,ties.method = 'first')==975)
abline(v = c(null.d[u1],null.d[u2]),col='red')
seq(1,length.out=10,by=3)
##############
# 1.
x <- 1:100
x %% 2 == 1
x[x %% 2 == 1]
sum(x[x %% 2 == 1])
# 2.
x <- rnorm(10,mean = 3, sd = 1)
n<-length(x)
mu <- sum(x)/n
v <- sum( (x-mu)^2)/ (n -1)
sigma <- sqrt(v)
se <- sigma/sqrt(n)
# 3.
md <- function(x){
n <- length(x)
ifelse(n %% 2 == 0, mean(x[c(n/2,n/2+1)]), x[(n+1)/2] )
}
#4.
integ <- function(a,b,f,d = 1e-5){
x0 <- seq(a,b,by = d)
sum(f(x0)*d)
}
integ(0,pi,sin)
integ(0,2*pi,sin,d = 1e-7)
# 5
dna.seg <- "ACTACCACTGACCACTACTGGGGGGGGCATATATATATATA"
gregexpr("ACT[ACTG]",dna.seg)#1
gregexpr("ACT[^AC]",dna.seg)#2
gregexpr("ACTG{2,5}",dna.seg)#3
gregexpr("GCA(TA){2,4}",dna.seg)#4
gregexpr("ACTG{3,}",dna.seg)#5
gregexpr("ACTG{,6}",dna.seg)#6
gregexpr("ACTG{5}",dna.seg)#7
gregexpr("(TA){3,5}",dna.seg)#8
# 6.
setwd("d:/rdata")
popdt <- read.csv("pop2019.csv")
attach(popdt)
population <- male + female #6.1
mfratio <- male/female # 6.2
obpr <- ifelse( year <= 2019, "Observed","Predicted")
newpopdt <- cbind(popdt,population,mfratio,obpr)
head(newpopdt)
write.csv(newpopdt, "newpopdata.csv")
year[population == max(population)] # 6.5
year[which.max(population)]
population[which.max(population)] # 6.5
library(ggplot2)
p <- ggplot(data = newpopdt, aes(x = year, y = population, color = obpr) )
p + geom_point()+
geom_line() +
geom_vline(xintercept = 2019)
p + geom_line( aes(y = male)) +
geom_line( aes(y = female)) ## 6.7
q <- ggplot(data = newpopdt, aes(x = year, y = mfratio, color = obpr) )
q + geom_point() + geom_line(lwd=4)