[R] string processing(regular expressions)

arun smartpink111 at yahoo.com
Sun Sep 1 23:18:29 CEST 2013


Hi,
 levels(nCourse)<-gsub("^0+","",levels(nCourse))
 nCourse
# [1] 2A   2B   2C   7A   7B   7C   101  118A 118B 118C
#Levels: 2A 2B 2C 7A 7B 7C 101 118A 118B 118C

#The second part is not very clear.
res<-setNames(data.frame(lapply(c("B","P","C"),function(x) paste0(levels(nCourse),x)),stringsAsFactors=FALSE),c("B","P","C"))
head(res)
#    B   P   C
#1 2AB 2AP 2AC
#2 2BB 2BP 2BC
#3 2CB 2CP 2CC
#4 7AB 7AP 7AC
#5 7BB 7BP 7BC
#6 7CB 7CP 7CC
A.K.




----- Original Message -----
From: Robert Lynch <robert.b.lynch at gmail.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Sunday, September 1, 2013 4:41 PM
Subject: [R] string processing(regular expressions)

I have a variable that is course #
nCourse <-
as.factor(c("002A","002B","002C","007A","007B","007C","101","118A","118B","118C"))

And I would like to get rid of the leading zeros, and have the following
set
("2A","2B","2C","7A","7B","7C","101","118A","118B","118C") to paste()
together with the department, "B","P","C" (bio, phys, & chem etc)

I am stuck trying to figure out regular expressions, they are new to me.

Thank You very much

    [[alternative HTML version deleted]]

______________________________________________
R-help at 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.




More information about the R-help mailing list