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