如何从数字向量中提取前4位数字?
game_ID <- c("201600768", "201600842", "201693456", "201700848", "201804567")
我的数据集中有一个列,其中包含许多数字,例如上面的数字。我想从每个数字中提取前4位数字(因为这是游戏发生的一年),然后将它们分成一个新列。
有什么建议吗?
game_ID <- c("201600768", "201600842", "201693456", "201700848", "201804567")
I have a column in my dataset that includes many numbers like the ones above. I would like to extract the first 4 digits from each number(because it is the year the game occurred), and separate them into a new column.
Any suggestions for going about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它们始终处于前四个位置,则可以在基本r中使用
substr
来识别位置:使用
如果您的数据是较大数据框中的列,则可以
:您可以简单地执行此操作。 :
输出:
If they are always in the first four positions, you can use
substr
in base R to identify the positions:Output
If your data are a column in a larger data frame, such as:
You can simply do this:
Output:
使用
Stringr
软件包到str_extract
前4位数字。您可以使用以下代码:输出:
Using the
stringr
package tostr_extract
the first 4 digits. You can use the following code:Output: