[问题类型]:
程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
[软件熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
hi大家好
我现在有10个档案,档名是 Sale1~10.csv
10个档案代表1~10年的销售资料
column都是一些产品名称
row是月份
现在我想要把固定一个产品的某个月份,观看十年来的差异
本来我用了split函数就能捞到我想看的10年的<产品+月份>
然后产生一个CSV档,我再用EXCEL拉出趋势图看 (EXCEL颜色比较漂亮Q_Q)
但是我主管想要自己去挑选<产品+月份>的组合自己跑R来看资料
我就必须要把程式写得比较弹性让他自行输入产品跟月份
最后一样只要产生一个CSV档给他自己拉趋势图看就好
[程式范例]:
#读档
year1 <- read.csv("D:/year1.csv")
year2 <- read.csv("D:/year2.csv")
year3 <- read.csv("D:/year3.csv")
year4 <- read.csv("D:/year4.csv")
year5 <- read.csv("D:/year5.csv")
year6 <- read.csv("D:/year6.csv")
year7 <- read.csv("D:/year7.csv")
year8 <- read.csv("D:/year8.csv")
year9 <- read.csv("D:/year9.csv")
year10 <- read.csv("D:/year10.csv")
#挑选想看的产品资料并依月分做切割
year1.Product1 = split(year1$Product1 , year1$Month)
year2.Product1 = split(year2$Product1 , year2$Month)
year3.Product1 = split(year3$Product1 , year3$Month)
year4.Product1 = split(year4$Product1 , year4$Month)
year5.Product1 = split(year5$Product1 , year5$Month)
year6.Product1 = split(year6$Product1 , year6$Month)
year7.Product1 = split(year7$Product1 , year7$Month)
year8.Product1 = split(year8$Product1 , year8$Month)
year9.Product1 = split(year9$Product1 , year9$Month)
year10.Product1 = split(year10$Product1 , year10$Month)
#挑出Product1在十年来July销售量
year1Product1July =year1.Product1$"July"
year2Product1July =year2.Product1$"July"
year3Product1July =year3.Product1$"July"
year4Product1July =year4.Product1$"July"
year5Product1July =year5.Product1$"July"
year6Product1July =year6.Product1$"July"
year7Product1July =year7.Product1$"July"
year8Product1July =year8.Product1$"July"
year9Product1July =year9.Product1$"July"
year10Product1July =year10.Product1$"July"
#存成dataframe后输出到csv
Product1July =
data.frame(year1Product1July,year2Product1July,year3Product1July,
year4Product1July,year5Product1July,year6Product1July,year7Product1July,
year8Product1July,year9Product1July,year10Product1July)
write.csv(Product1July, file="D:/Product1July.csv")
我尝试着先以 #挑选想看的产品资料并依月分做切割
写了for循环搭配paste结果失败了
Product = "Product1" #可自行输入产品编号
Month = "July" #可指定月份
for(i in 1:10){
x = paste("year",i,Product,sep="");
y = paste("year",i,"$",Product,sep="");
z = paste("year",i,"$",Month,sep="");
x = split(y,z);
}
请问我上面程式要怎么修改才能让做到像上面那些指令的结果呢
感觉是split认为paste出来的东西不是object
我想要把上面这些写死的script写得更弹性简洁一些
求高人指点 谢谢
[关键字]:
选择性,也许未来有用