开发者

Extracting first letter from specific vector in dataset in R

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), 
                   开发者_如何学Python                         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?


I might not get the question, but:

substr(babies2009$name, 1, 1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜