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
;character" "integer" > strsplit(d1$name, " ") [[1]] [1] "Bill" "Dunlap" [[2]] [1] "First" "Last" Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com -

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

2009-04-10 Thread Mao Jianfeng
Dear, R-lister, I have a dataframe like the followed. And, I want to split a character variable ("popcode", or "codetot") into several new variables. For example, split "BCPy01-01" (in popcode) into "BCPy01" and "01". I need to know how to do that. I have tried strsplit() and substring() functions

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