I am currently being defeated by grep. I am attempting to determine the value
of the last letter of a character string.
An example of my data set is shown below. Regarding the codes, I would like to
identify the value of the last character and then take the appropriate action,
e.g.
If the value is L then label UL rating XXX
It the value is F then label UL rating YYY
...
I assume it will be something like the following:
grep("last letter", HousesWithCodes$Codes)
Thanks again for any insights.
BuildYear<-c(1980, 1985, 1975, 1990, 1980)
SqrFootage<-c(1500, 1650, 1500, 2000, 1450)
Exterior<-c("Brick", "Stone", "Siding",
"Brick", "Siding")
SubdivisionHouses<-data.frame(BuildYear, SqrFootage, Exterior)
Year<-c(1980, 1985, 1975, 1990, 1977, 1986)
Codes<-c("G2G1L", "G5L1F", "K1Y2G",
"G4B1B", "K1N3B", "K3M4X")
BuildingCodes<-data.frame(Year, Codes)
HousesWithCodes<-merge(SubdivisionHouses, BuildingCodes,
by.x="BuildYear", by.y="Year")
Well, regexpr() can do it; the magical incantation is
regexpr(".$",yourstring)
See ?regexpr for details.
However, as your task really doesn't involve MATCHING characters, but
COUNTING characters, it might be simpler to use nchar() and substr():
n <- nchar(yourstring)
lastLetter <- substr(yourstring, n, n)
Bert Gunter
Genentech Nonclinical Biostatistics
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Jason Rupert
Sent: Monday, October 19, 2009 12:52 PM
To: R-help at r-project.org
Subject: [R] Using grep to determine value of last letter...
I am currently being defeated by grep. I am attempting to determine the
value of the last letter of a character string.
An example of my data set is shown below. Regarding the codes, I would like
to identify the value of the last character and then take the appropriate
action, e.g.
If the value is L then label UL rating XXX
It the value is F then label UL rating YYY
...
I assume it will be something like the following:
grep("last letter", HousesWithCodes$Codes)
Thanks again for any insights.
BuildYear<-c(1980, 1985, 1975, 1990, 1980)
SqrFootage<-c(1500, 1650, 1500, 2000, 1450)
Exterior<-c("Brick", "Stone", "Siding",
"Brick", "Siding")
SubdivisionHouses<-data.frame(BuildYear, SqrFootage, Exterior)
Year<-c(1980, 1985, 1975, 1990, 1977, 1986)
Codes<-c("G2G1L", "G5L1F", "K1Y2G",
"G4B1B", "K1N3B", "K3M4X")
BuildingCodes<-data.frame(Year, Codes)
HousesWithCodes<-merge(SubdivisionHouses, BuildingCodes,
by.x="BuildYear",
by.y="Year")
______________________________________________
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.
Gabor Grothendieck
2009-Oct-19 20:14 UTC
[R] Using grep to determine value of last letter...
Here are several ways to find the last character in a string:> x <- "abc"> substring(x, nchar(x))[1] "c"> sub(".*(.)", "\\1", x)[1] "c"> library(gsubfn) > strapply(x, ".$")[[1]][1] "c" On Mon, Oct 19, 2009 at 3:52 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:> I am currently being defeated by grep. ?I am attempting to determine the value of the last letter of a character string. > > An example of my data set is shown below. ?Regarding the codes, I would like to identify the value of the last character and then take the appropriate action, e.g. > If the value is L then label UL rating XXX > It the value is F then label UL rating YYY > ... > > I assume it will be something like the following: > grep("last letter", ?HousesWithCodes$Codes) > > Thanks again for any insights. > > BuildYear<-c(1980, 1985, 1975, 1990, 1980) > SqrFootage<-c(1500, 1650, 1500, 2000, 1450) > Exterior<-c("Brick", "Stone", "Siding", "Brick", "Siding") > > SubdivisionHouses<-data.frame(BuildYear, SqrFootage, Exterior) > > Year<-c(1980, 1985, 1975, 1990, 1977, 1986) > Codes<-c("G2G1L", "G5L1F", "K1Y2G", "G4B1B", "K1N3B", "K3M4X") > BuildingCodes<-data.frame(Year, Codes) > > HousesWithCodes<-merge(SubdivisionHouses, BuildingCodes, by.x="BuildYear", by.y="Year")