2

I am working in R with a babyname dataset. The data set contains a column called name: babies1900$name. I know how to extract the last letter from each row in vector name.

last.letter.1900 <- substr(babies1900$name, nchar(babies1900$name), 
                                            nchar(babies1900$name))

I was interested in doing the same for the first letter for a dataset from a different year. This is what I did, but am wondering if there a more "elegant" way of doing it.

x = babies2009$name

x = nchar(x)

y= x + 1

z = y -x

z =
[1] 1

babies.test = substr(babies2009$name, z, z)

This totally works, but is there a different way?

joran
  • 169,992
  • 32
  • 429
  • 468
akz
  • 1,865
  • 2
  • 16
  • 13

1 Answers1

3

I might not get the question, but:

substr(babies2009$name, 1, 1)
daroczig
  • 28,004
  • 7
  • 90
  • 124