一、导入数据
- reasxl包:功能是导入excel
- tianmaoTV位置:放在当前script所在文件下
- skip=1:即跳过表的第1行
install.packages('readxl') #下载读取excel的包
library(readxl) #加载到内存
tianmao<-read_excel('tianmaoTV.xlsx',skip=1) #导入,不导第1行
结果选示:
已导入的tianmaoTV有18个字段
二、增加列
- ifelse()函数:
- R的优势:可以通过下面方式直接实现向量对应位置相乘相除
#生成新列:total_sales
tianmao['total_sales']<-tianmao$current_price * tianmao$month_sales_count
#生产新列:zhekou
tianmao$zhekou<-tianmao$current_price/tianmao$original_price
#生成新列price_class
#小于1000,低价;(1000,2000),适中;2000以上,高价
tianmao['price_class']<-ifelse(tianmao$cur