[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
一、Factor函数
#函数factor可以把一个向量编码为一个因子,其一般形式为:
#factor(x,levels=sort(unique(x),na.last=TRUE),labels,exculde=NA,order=FALSE)
#其中x是向量,levels是水平,可以自行指定各离散的取值,不指定时由x的不同值来表示,labels可以用来指定各水平的标签
#不指定时用各离散取值的对应字符串 sex<-c("M","F","M","M","F") factor(sex)
#使用is.factor来判断对象是否为因子类型 is.factor(sex) #使用as.factor来将对象转换为因子类型
sex.factor<-as.factor(sex) #使用levels来取得因子中的水平 levels(sex.factor) # [1] "F" "M"
二、tapply函数
#函数tapply() #当我们知道5个人的性别以及5个人的身高的时候,就能计算出每个性别下的平均身高
height<-c(170,175,180,165,168) tapply(height,sex.factor,mean) # F M # 171.5000
171.6667 #tapply 的使用格式为tapply(x,index,fun) 其中x为对象,index为与x具有相同个数的因子类型,fun为指定的方法
三、gl()函数
#gl()函数 #gl函数能很快的产生因子,其基本用法为:gl(n,k,length=n*k,labels=1:n,order=false) gl(3,5)
# [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 # Levels: 1 2 3 gl(3,1,15) # [1] 1 2 3 1 2
3 1 2 3 1 2 3 1 2 3 # Levels: 1 2 3