Re: [問題] 利用文字向量做索引選取群組產生列表
※ 引述《Nonen (1800g)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 請把以下不需要的部份刪除
: 新手(沒寫過程式,R 是我的第一次)
: [問題敘述]:
: 請簡略描述你所要做的事情,或是這個程式的目的
: 我正在作一個能夠回傳有修高級車品牌的修車廠名字的程式,目前我已經有了372家修車
: 場的資料並整理出了兩個向量。
: 向量一:只有修車廠名字
: 例如 c(甲,乙,丙,...)
: 向量二:車廠名字和有修的車子品牌 例如 c(甲,a,b,c,乙,b,f,g,丙,c,h,i,..)
: 然後假設高級車品牌有c(a,b,..),我想要找出任何有修高級車品牌的車廠
: 目前我卡在如何將向量二分成372家車廠的資料,因為每家會修的品牌數不一樣無法使用
: 一定的間隔做切割
: 目前理想是可以做出一張list裡面包含372個sublist且用店家名命名,每個sublist裡有
: 該店家有修的車子品牌,再去看有哪些店家(回傳sublist name)裡有包含任一甚至更多高
: 級車品牌名單。還沒有想到很好的解法,先謝謝大家了!
: [程式範例]:
: 車廠名單示意圖(向量一)
: http://imgur.com/a/Awtuj
: 維修資訊示意圖(向量二)
: http://imgur.com/a/zLqOi
: [環境敘述]:
: R version 3.3.1 (2016-06-21)
: Platform: x86_64-apple-darwin13.4.0 (64-bit)
: Running under: OS X 10.11.3 (El Capitan)
: [關鍵字]:
: 選擇性,也許未來有用
圖二看不到,我先根據你意思先生成資料
好讀版:http://pastebin.com/kgtr0RHX
# 生成資料的函數就比較複雜一點,需要一點時間研究
# 用到的就paste0, sprintf, replicate, sample, unlist, mapply這幾個而已
# ?查一下應該不用太久時間就可以知道他們在幹嘛了
# 至於要會用就要慢慢經驗累積 try and error了
firms <- paste0("Firm", sprintf("%03i", 1L:500L))
brands <- paste0("Brand", sprintf("%03i", 1L:100L))
brandsList <- replicate(length(firms), sample(brands, sample(1L:20L, 1L)))
brandsFirms <- unlist(mapply(function(x, y) c(x, y), firms, brandsList,
USE.NAMES = FALSE))
head(brandsFirms, 24L)
# [1] "Firm001" "Brand046" "Brand024" "Brand021" "Brand050" "Brand011"
# [7] "Brand078" "Brand003" "Brand061" "Brand016" "Brand085" "Brand057"
# [13] "Firm002" "Brand006" "Brand072" "Brand024" "Brand057" "Brand083"
# [18] "Brand082" "Brand044" "Brand099" "Brand075" "Firm003" "Brand058"
# 所以firms1是你的vector 1,brandsFirms是你的vector 2
# 下面使用的function都是base裡面的,隨便找本基本R書都有教
# match, findInterval, split等,沒教那本書就可以丟了
# seq_along, head應該都不用特別教吧,自己把玩一下就會了~~~
loc <- match(firms, brandsFirms)
idx <- findInterval(seq_along(brandsFirms), loc)
idx_firms <- firms[idx]
idx_firms[loc] <- "firms"
output <- split(brandsFirms, idx_firms)
output$firms <- NULL
head(output, 3L)
# $Firm001
# [1] "Brand046" "Brand024" "Brand021" "Brand050" "Brand011" "Brand078"
# [7] "Brand003" "Brand061" "Brand016" "Brand085" "Brand057"
#
# $Firm002
# [1] "Brand006" "Brand072" "Brand024" "Brand057" "Brand083" "Brand082"
# [7] "Brand044" "Brand099" "Brand075"
#
# $Firm003
# [1] "Brand058" "Brand069" "Brand047"
測試看看吧,有問題再上來問
比較閒就上個顏色,資料preview用綠色,註解用黃色~~
一個小建議是以後資料可以直接貼上來
這樣我可以直接手抓下來...
不然我就會生成虛擬資料,你實際case的資料就要自己再測試
--
R資料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.170.44.139
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1487962634.A.F55.html
推
02/25 09:44, , 1F
02/25 09:44, 1F
→
02/25 09:44, , 2F
02/25 09:44, 2F
整數跟浮點數的差異
typeof(1L) # integer
typeof(1) # double
→
02/25 12:01, , 3F
02/25 12:01, 3F
→
02/26 13:06, , 4F
02/26 13:06, 4F

→
02/26 13:14, , 5F
02/26 13:14, 5F
→
02/26 13:14, , 6F
02/26 13:14, 6F
→
02/26 13:15, , 7F
02/26 13:15, 7F
※ 編輯: celestialgod (36.232.184.141), 03/05/2017 19:36:11
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章