Re: [R] split a character variable into several character variable by a character

2009-04-11 Thread Darren Norris
just an alternative try gsub ?gsub from Francisco's example: dat<-read.table("clipboard", header=T)#Read from your email gsub("-.*","",as.character(dat$popcode))# gives the BCPy01 part of column popcode gsub(".*-","",as.character(dat$popcode)) # gives the 01 part of column popcode then to

Re: [R] split a character variable into several character variable by a character

2009-04-10 Thread Francisco J. Zagmutt
Hello Mao, If the popcode variable has a fixed number of characters (i.e each entry has 9 characters), you can use a simple call to substr: dat<-read.table("clipboard", header=T)#Read from your email varleft<-substr(dat$popcode,0,6) varright<-substr(dat$popcode,8,9) datnew<-data.frame(dat,varl

Re: [R] split a character variable into several character variable by a character

2009-04-10 Thread William Dunlap
strsplit() is the way to do it, but if your putative character strings come from a data.frame you need to make sure they are really character strings and not factors (at least in R 2.8.1). > d<-data.frame(name=c("Bill Dunlap", "First Last"), num=1:2) > d name num 1 Bill Dunlap

Re: [R] split a character variable into several character variable by a character

2009-04-10 Thread Adrian Dusa
Dear Mao Jianfeng, "r-help-owner" is not the place for help, but: r-help@r-project.org (CC-ed here) In any case, strsplit() does the job, i.e.: > unlist(strsplit("BCPy01-01", "-")) [1] "BCPy01" "01" You can work with the whole variable, like: splitpop <- strsplit(df1$popcode, "-") then access t