书籍:《R语言与数据挖掘》
library(reshape)
CO2
CO2 <- rename(CO2,c(Treatment = "Treat"))
> anyNA(CO2)
[1] FALSE
> sort(CO2$uptake,decreasing = TRUE)
[1] 45.5 44.3 43.9 42.9 42.4 42.1 41.8 41.4 41.4 40.6 40.3 39.7
[13] 39.6 39.2 38.9 38.8 38.7 38.6 38.1 37.5 37.2 37.1 35.5 35.4
[25] 35.3 35.0 34.8 34.6 34.0 32.5 32.4 32.4 32.4 31.8 31.5 31.1
[37] 30.9 30.6 30.4 30.3 30.0 28.5 28.1 27.9 27.8 27.3 27.3 26.2
[49] 25.8 24.1 22.2 22.0 21.9 21.0 19.9 19.5 19.4 19.2 18.9 18.9
> sort(CO2$uptake,decreasing = FALSE)
[1] 7.7 9.3 10.5 10.6 10.6 11.3 11.4 12.0 12.3 12.5 13.0 13.6
[13] 13.7 14.2 14.4 14.9 15.1 16.0 16.2 17.9 17.9 17.9 18.0 18.1
[25] 18.9 18.9 19.2 19.4 19.5 19.9 21.0 21.9 22.0 22.2 24.1 25.8
[37] 26.2 27.3 27.3 27.8 27.9 28.1 28.5 30.0 30.3 30.4 30.6 30.9
[49] 31.1 31.5 31.8 32.4 32.4 32.4 32.5 34.0 34.6 34.8 35.0 35.3
> CO2[order(CO2$uptake),]
Plant Type Treat conc uptake
71 Mc2 Mississippi chilled 95 7.7
29 Qc2 Quebec chilled 95 9.3
64 Mc1 Mississippi chilled 95 10.5
43 Mn1 Mississippi nonchilled 95 10.6
78 Mc3 Mississippi chilled 95 10.6
57 Mn3 Mississippi nonchilled 95 11.3
> CO2[order(-CO2$uptake),]
Plant Type Treat conc uptake
21 Qn3 Quebec nonchilled 1000 45.5
14 Qn2 Quebec nonchilled 1000 44.3
20 Qn3 Quebec nonchilled 675 43.9
19 Qn3 Quebec nonchilled 500 42.9
35 Qc2 Quebec chilled 1000 42.4
n <- sample(2,84,replace = TRUE,prob = c(0.6,0.4))
(sample1 <- CO2[n == 1,])
(sample2 <- CO2[n == 2,])
tapply(CO2$uptake,CO2$Plant,mean)
aggregate(CO2$uptake,by = list(CO2$Plant,CO2$Type),FUN = mean)
lapply(c(CO2$conc,CO2$uptake),mean)
Plant_Qn <- grep("Qn",CO2$Plant,fixed = FALSE)
Plant_Qn
gsub("[t]","t",CO2$Plant)
library(fBasics)
stat <- function(x)
{
if(!is.numeric(x))
{
stop("the input data must be numeric!\n")
}
if(length(x) == 1)
{
stop("can not compute sd for one number!\n")
}
max1 <- max(x)
min1 <- min(x)
mean1 <- mean(x)
skewness1 <- skewness(x)
kurtosis1 <- kurtosis(x)
answer <- c(max1,min1,mean1,skewness1,kurtosis1)
return(answer)
}
t <- rt(100,2)
stat(t)
|