R语言——基础学习——第四课

本文详细介绍如何使用R语言进行数据读取、预处理、变量创建、筛选及子集提取等操作,通过具体案例演示了从数据导入到分析的全过程,特别关注于价格分析和品牌筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

install.packages("readxl")
getwd()
setwd("E:/作业4")
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
tianmao$zhekou

a<-1:10
ifelse(a%%2==0,'偶数', '奇数')

tianmao['price_class']<-ifelse(tianmao$current_price<1000,'低价',
                              ifelse(tianmao$current_price<=2000,"适中",'高价'))
tianmao[c('price_class','current_price')]

#重命名
names(tianmao)
names(tianmao)[1]<-'mingcheng'

%in% #判断符号前的向量的元素是否是在符号后面的向量中,返回逻辑向量,如果在后面向量中,就返回true
names(tianmao)%in%"weight" #是true的就说明这个元素在weight里面
names(tianmao)[names(tianmao)%in%"weight"]
names(tianmao)[names(tianmao)%in%"weight"]<-'zhongliang'
names(tianmao)

#提取子集
newdata<-tianmao[,-c(1:3)]
names(newdata)
names(tianmao)

col1<-c('mingcheng','description','current_price')
logical<-names(tianmao)%in%col1
newdata1<-tianmao[,!logical]
names(newdata1)

tianmao[1,]
logical1<-tianmao$brand=='Xiaomi/小米'
xiaomi<-tianmao[logical1,]
xiaomi

?subset
xiaomi1<-subset(tianmao,brand=='Xiaomi/小米',c("mingcheng","description"))


#homework Q1
library(readxl)
tianmao_2 <- read_excel("E:/作业4/tianmaoTV.xlsx",skip = 1)
View(tianmao_2)

#homewotk Q2
logical<-tianmao_2$current_price<1000
tianmao_Q2<-tianmao_2[logical,]
View(tianmao_Q2)


#homework Q3
tianmao_2$stock_class<-ifelse(tianmao_2$stock==0,'无货',
                                ifelse(tianmao_2$stock<100,'低库存','高库存'))
View(tianmao_2)

#homework Q4
tianmao_Q4<-tianmao_2[c('stock','stock_class')]
tianmao_Q4

#homewotk Q5
names(tianmao_2)
tianmao_Q5<-tianmao_2[,-c(7,8)]
names(tianmao_Q5)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值