[問題] 想連續處理多個column by factor
看板R_Language作者peiwinds (Macchiato Caramel)時間11年前 (2013/10/24 10:47)推噓0(0推 0噓 5→)留言5則, 2人參與討論串1/4 (看更多)
[問題類型]:
程式諮詢(我想用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寫得更彈性簡潔一些
求高人指點 謝謝
[關鍵字]:
選擇性,也許未來有用
--
┐ * ┐ *
* │ │ ╭──╮ ╭──╮ *╭──╮
├──╯ ┘ │ ┐*│ │ *│ ◣ ◢
│ *╮ ╭──┤* ╭┼─╯ ├──╯* █▌
│ │ │* │ │ ┐ │ ┐ * ◣S
┘ ┘ ╰──╰ ╰──╯ ╰──╯
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.96.100.10
→
10/24 12:11, , 1F
10/24 12:11, 1F
→
10/24 12:12, , 2F
10/24 12:12, 2F
→
10/24 12:12, , 3F
10/24 12:12, 3F
→
10/24 12:26, , 4F
10/24 12:26, 4F
→
10/24 13:07, , 5F
10/24 13:07, 5F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 4 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章