R data analysis 2

R language exercises

Practice problem: Find the number of daffodils

nums<- 100:999
first.num <- nums%/%100
second.num<-(nums%%100)%/%10
third.num<-nums%%10           
nums[(first.num^3 + second.num^3 + third.num^3) == nums]
nums<- 1000:9999
first.num <-nums%/%1000
second.num <-(nums%%1000)%/%100
third.num <- (nums%%100)%/%10
forth.num <-nums%%10
nums[(first.num^4 +second.num^4 +third.num^4+forth.num^4) ==nums]

Practice question: find all factors of a number

num <-6
c <- 1:6
c[6%%c ==0]

Practice question: Determine whether a number is finished. (Sum function sun())

nums <-1:(6/2)
sum(nums[6%%nums ==0]) ==6

matrix

创建矩阵
m<-matrix(1:8,nrow = 2)
矩阵运算
m1 <- matrix(1:4, nrow = 2)
m2<- matrix(2:5, nrow = 2)
m1;m2
m1 + m2
m1 * m2
矩阵索引
m<- matrix(1:12, nrow = 4)
m
m[,2:3]
m[ 1:3,]#一定要记得逗号
m[1:3]
m[1:5]
删除矩阵元素
改变矩阵的值
m <- matrix (1:12,nrow = 4)
m
m[-1,]
m[,-1]
m[-1,-1]

m
m.value <-matrix(rep(0,6), nrow=2)
m.value
m[c(1,3),]
m[c(1,3),] <- m.value
m

File saving: the file and the script are in the same path

R数据应用:修改图片
install.packages("pixmap")#导入包
library(pixmap)
p <- read.pnm("python.pgm")#读取本地照片文件,
#注意:文件与与脚本在同一路径下
p
plot(p)
str(p)
p2 <-p
p2@grey[80:103,10:60] <-1
plot(p2)

Array

test1 <- matrix(sample(90:100,6,replace = F),nrow = 2)
test1
test2 <-matrix(sample(90:100,6,replace = F),nrow = 2)
test2
test <- array(data=c(test1,test2),dim = c(2,3,3))
test
attributes(test)
str(test)
test[,,1]
test[1,,1]
test[,1,1]
test[1,1,1]

List

创建列表
stu.lst <- list(name = "xiaoming", id=1001,is.past.exam = T)
stu.lst
stu.lst2 <- list("xiaoming",1001,T)#可以不给列表加标签
stu.lst2
new.l <- vector(mode = "list")
new.l
new.l[["name"]] <- "xiaoming"
new.lstu.lst <- list(name = c("xiaoming","xiaohua"),id=1001,is.past.exam = T);stu.lst
列表的常规操作
#列表索引
stu.lst
stu.lst$name
stu.lst[["name"]]
stu.lst["name"]
name <- stu.lst["name"]
name
str(name)
name2 <- stu.lst[["name"]]
name2
str(name2)#双中括号一次提取的是列表的一个组件,返回值是组件本身的类型
stu.lst[[1]]
stu.lst[1]
stu.lst[1:2]#对原列表取子集
stu.lst[c(1,3)]
#列表添加元素
stu.lst
stu.lst$age <-19
stu.lst
length(stu.lst)#获取列表的长度
stu.lst[[5]] <-98
stu.lst
stu.lst[6:8] <- 1:3
stu.lst
#列表删除元素
stu.lst
stu.lst[[8]] <- NULL
stu.lst
stu.lst[6:7] <-NULL
stu.lst
stu.lst$age <- NULL
stu.lst
#将多个列表拼接为一个列表
stu.lst
stu.lst2
c(stu.lst,stu.lst2)
#访问列表元素的标签
stu.lst
names(stu.lst)
#去掉向量的元素名
stu.lst
names(stu.lst) <- NULL
stu.lst

Data frame

创建数据框
stu.name <-c("xinyu" , "anqi")
stu.id <- c (1001,1002)
stu.info <- data.frame (stu.name,stu.id) ; stu.info
str (stu.info)
数据框的相关操作
#访问
stu.info$stu.name
stu.info[,1]
stu.info[1,]
#添加新列
stu.info$age <- c(16,19);stu.info

#查询
stu.info[which(stu.info$age>=16),]
stu.info[stu.info$age>=16,]

#矩阵转数据框
mat <-matrix(1:12,nrow=3);mat
mat.df<-as.data.frame (mat);mat.df
#列表的高级操作
#merge
stu.id.math <- c (1001,1003)
stu.math.score <-c(89,90)
stu.math.df <- data.frame(stu.id.math,stu.math.score);stu.math.df
?merge
merge (stu.info,stu.math.df,by.x = "stu.id",by.y = "stu.id.math")
#合并
#cbind, rbind
stu.info
new.stu <- data.frame(list(stu.name="xiaohua",stu.id=1004,age=19));new.stu
stu.info <- rbind(stu.info, new.stu) ;stu.info
python.score <- c(99,100,100)
stu.info <-cbind (stu.info, python.score) ; stu.info
r.score <- c(98,99,100)
stu.info <-cbind (stu.info,r.score);stu.info
lapply(stu.info,max)
lapply(stu.info[3 :ncol(stu.info)], max)
sapply(stu.info[3: ncol (stu.info)], max)

factor

#创建因子
sex <-c(1,2);sex
sex.f <- factor (sex);sex.f
str (sex.f)
stu.sex <- sample(1:2,10,replace = T); stu.sex
stu.sex.f<-factor(stu.sex); stu.sex.f
stu.sex.f<- factor(stu.sex, levels = c(1,2));stu.sex.f
stu.sex.f<- factor(stu.sex,levels = c(1,2),labels = c("E","M"));stu.sex.f
stu.sex.v<- as.vector(stu.sex.f);stu.sex.v
有序因子
stu.sex.ord <- ordered(stu.sex,levels = c(1,2));stu.sex.ord
stu.sex.ord <- ordered(stu.sex,levels = c(2,1));stu.sex.ord
#cut函数
stu.r.score <- c(60,sample(60:100,8,replace = T),100);stu.r.score
stu.r.score.level <- cut(stu.r.score,breaks = 4);stu.r.score.level
#tapply()函数
tapply(stu.r.score,stu.sex, mean)

Insert picture description here
Doraemon welcomes you!

Guess you like

Origin blog.csdn.net/m0_50150737/article/details/108567861