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,varleft,varright)

> datnew
     popcode     codetot   p3need varleft varright
1  BCPy01-01 BCPy01-01-1 100.0000  BCPy01       01
2  BCPy01-01 BCPy01-01-2 100.0000  BCPy01       01
3  BCPy01-01 BCPy01-01-3 100.0000  BCPy01       01
4  BCPy01-02 BCPy01-02-1  92.5926  BCPy01       02
5  BCPy01-02 BCPy01-02-1 100.0000  BCPy01       02
6  BCPy01-02 BCPy01-02-2  92.5926  BCPy01       02
7  BCPy01-02 BCPy01-02-2 100.0000  BCPy01       02
8  BCPy01-02 BCPy01-02-3  92.5926  BCPy01       02
9  BCPy01-02 BCPy01-02-3 100.0000  BCPy01       02
10 BCPy01-03 BCPy01-03-1 100.0000  BCPy01       03


You can use a similar construction for codetot.

I hope this helps,

Francisco

Francisco J. Zagmutt
Vose Consulting
2891 20th Street
Boulder, CO, 80304
USA
franci...@voseconsulting.com
www.voseconsulting.com

Mao Jianfeng wrote:
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. But, I still can
not perform the spliting.

Any advice? Thanks in advance.

df1:
popcode     codetot   p3need
BCPy01-01 BCPy01-01-1 100.0000
BCPy01-01 BCPy01-01-2 100.0000
BCPy01-01 BCPy01-01-3 100.0000
BCPy01-02 BCPy01-02-1  92.5926
BCPy01-02 BCPy01-02-1 100.0000
BCPy01-02 BCPy01-02-2  92.5926
BCPy01-02 BCPy01-02-2 100.0000
BCPy01-02 BCPy01-02-3  92.5926
BCPy01-02 BCPy01-02-3 100.0000
BCPy01-03 BCPy01-03-1 100.0000

Regards,

Mao Jian-feng

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to