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
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
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
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
4 matches
Mail list logo