作者celestialgod (天)
看板R_Language
標題Re: [問題] 資料切割問題請益
時間Wed Aug 26 09:35:38 2015
※ 引述《funny4875 (愛落紅塵心已死)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 新手,約1~2月
: [問題敘述]:
: data如下
: Params1
: android 4.4.2/TW/ver 1.5.6
: android 5.0.2/TW/ver 1.5.6
: android 5.0/TW/ver 1.5.6
: iphone 8.4/EN/ver 1.6.0
: .
: .
: .
: .
: 我想要抓出資料中兩個/內的語系ex:TW EN
: 因為資料不對齊所以無法直接用substring去抓
: 後來我用
: data$Params1<- as.character(data$Params1)
: tt1 <- strsplit(data$Params1,"/")
: sapply(tt1,head,2)[[2]]
: > sapply(tt1,head,2)[[2]]
: [1] "TW"
: 只有跑出一個"TW",不知道為何?
: 想問哪裡出了問題
: 或是有更適合的function可以使用?
: 謝謝~
: [關鍵字]:
: strsplit,資料切割
試著用regular expression做吧
dat = c("android 4.4.2/TW/ver 1.5.6", "android 5.0.2/TW/ver 1.5.6",
"android 4.4.2/TW/ver 1.5.6", "iphone 8.4/EN/ver 1.6.0")
gsub('/', '', regmatches(dat, regexpr("/[A-Z]{2}/", dat)))
strsplit也可以:
sapply(strsplit(dat, '/'), '[', 2)
or
do.call(rbind, strsplit(dat, '/'))[,2]
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.27.107
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1440552942.A.764.html
※ 編輯: celestialgod (123.205.27.107), 08/26/2015 09:37:12
推 cywhale: 推c大的撇步有夠多~ 08/26 09:43
推 remember69: 已學習!!! 08/26 09:48