开发者

Extracting first letter from specific vector in dataset in R

开发者 https://www.devze.com 2023-04-06 02:50 出处:网络
I am working in R with a babyname dataset. The data set contains a column called name: babies1900$name.

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

精彩评论

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

关注公众号