作者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://webptt.com/cn.aspx?n=bbs/R_Language/M.1440552942.A.764.html
※ 编辑: celestialgod (123.205.27.107), 08/26/2015 09:37:12
1F:推 cywhale: 推c大的撇步有够多~ 08/26 09:43
2F:推 remember69: 已学习!!! 08/26 09:48