对这个文件进行操作,文件链接放下面。
链接:https://pan.baidu.com/s/1xZHWBKI227OUmt1MzM4YoA
提取码:g1rt
library('readxl') 加载包 没安装的先下载
#读取数据
tianmao<-read_excel("tianmaoTV.xlsx",skip = 1)
#创建新变量
#总销售额=价格*月销量
tianmao['total_sales']<-tianmao$current_price*tianmao$month_sales_count
tianmao[c('current_price','month_sales_count','total_sales')]#计算折扣 当前价格/原价格
tianmao$zhekou<-tianmao$current_price/tianmao$original_price#对价格进行分类,高于2000为高价,1000以内是便宜
#了解ifelse 用法
tianmao['price_class']<-ifelse(tianmao$current_price<1000,'低价',
ifelse(tianmao$current_price<=2000,'适中','高价'))
tianmao[c('price_class','current_price')]
#查看多列
tianmao[c('price_class','current_price')]
#重命名
names(tianmao)[1]<-'mingcheng'
print(names(tianmao))
#如何确定列名在那一列
names(tianmao)%in%"weight"
#如图 所以在第十二列
#重命名
names(tiaomao)[names(tianmao)%in%"weight"]<-'zhongliang'
#提取子集
#这部分方法和前面说的一样,不再过多进行解释
newdata<-tianmao[,-c(1:3)]#重新赋值,剔除数据
newdata
可以看到前三列已经没了
#另一种方法
col1<-c("mingcheng","description","current_price")
logical<-names(tianmao)%in%col1
newdata1<-tianmao[,!logical]
newdata1#返回第一行
tianmao[1,]
#筛选品牌是小米的商品数据
logical1<-tianmao$brand=="Xiaomi/小米"
xiaomi<-tianmao[logical1,]
xiaomi
#提取子集 使用subset()
Xiaomi1<-subset(tianmao,brand=="Xiaomi/小米",c(current_price))
#蓝色部分可选,默认是全部数据 c(current_price)只提取一列