首页 > 代码库 > R 笔记
R 笔记
(1)
> x = 11
> x
[1] 11
> print(x)
[1] 11
> y <- 7
> ls()
[1] "x" "y"
> rm(y)
> y <- 9
> x1 = c(1,3,5,7,9)
> x1
[1] 1 3 5 7 9
> sqrt(y)
[1] 3
> log(y)
[1] 2.197225
> log2(y)
[1] 3.169925
> exp(y)
[1] 8103.084
> 2:7
[1] 2 3 4 5 6 7
> seq(1,7)
[1] 1 2 3 4 5 6 7
> seq(1,7,2)
[1] 1 3 5 7
> seq(1,7,0.1)
> rep(‘martin‘, 10)
[1] "martin" "martin" "martin" "martin" "martin" "martin" "martin"
[8] "martin" "martin" "martin"
> x1
[1] 1 3 5 7 9
> x2=c(2,4,6,8,10)
> x1 + x2
[1] 3 7 11 15 19
> x1[3]
[1] 5
> x1[-3]
[1] 1 3 7 9
> x1[1:3]
[1] 1 3 5
> x1[c(1,5)]
[1] 1 9
> x1[x1<6]
[1] 1 3 5
(2)
> mat = matrix(c(1:9),3)
> mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> mat = matrix(c(1:9),3, byrow=FALSE)
> mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> mat[c(1,3),2]
[1] 4 6
> mat[2,]
[1] 2 5 8
(3)
> data1 = read.csv(file.choose())
> data2 = read.table(file.choose(), header= T, sep=",")
data3 = read.delim(file.choose(), header=T) //read tab delim file = > data4 = read.table(file.choose(), header= T, sep="\t")
dim(data2)
head(data2) //first six lines
tail(data2)
(4)
> mean(V2)
Error in mean(V2) : object ‘V2‘ not found
> mean(data$V2)
[1] 73.57143
> data$V2
> attach(data) //save data in memory
> mean(V2)
[1] 73.57143
detach(data)
> class(V1)
[1] "factor"
> class(V2)
[1] "integer"
> class(V4)
[1] "logical"
> class(V5)
[1] "factor"
> summary(data)
V1 V2 V3 V4 V5
overcast:4 Min. :64.00 Min. :65.00 Mode :logical no :5
rainy :5 1st Qu.:69.25 1st Qu.:71.25 FALSE:8 yes:9
sunny :5 Median :72.00 Median :82.50 TRUE :6
Mean :73.57 Mean :81.64 NA‘s :0
3rd Qu.:78.75 3rd Qu.:90.00
Max. :85.00 Max. :96.00
> x=c(1,0,1,1,1,0,1)
> class(x)
[1] "numeric"
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.5000 1.0000 0.7143 1.0000 1.0000
> x = as.factor(x)
> class(x)
[1] "factor"
> summary(x)
0 1
2 5