从r中的数据集中的特定矢量提取第一个字母
我正在 R 中处理婴儿名字数据集。 该数据集包含一个名为 name 的列:babies1900$name
。 我知道如何从向量名称的每一行中提取最后一个字母。
last.letter.1900 <- substr(babies1900$name, nchar(babies1900$name),
nchar(babies1900$name))
我有兴趣对不同年份的数据集的第一个字母执行相同的操作。 这就是我所做的,但我想知道是否有更“优雅”的方式来做到这一点。
x = babies2009$name
x = nchar(x)
y= x + 1
z = y -x
z =
[1] 1
babies.test = substr(babies2009$name, z, z)
这完全可行,但是还有其他方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能不明白这个问题,但是:
I might not get the question, but: