R语言(3) 数据框操作

对这个文件进行操作,文件链接放下面。

 链接: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)只提取一列

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

面咋了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值