提取字符串的前(或最后)n 个字符 [英] Extract the first (or last) n characters of a string

查看:38
本文介绍了提取字符串的前(或最后)n 个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取字符串的第一个(或最后一个)n 个字符.这相当于 Excel 的 LEFT()RIGHT().一个小例子:

I want to extract the first (or last) n characters of a string. This would be the equivalent to Excel's LEFT() and RIGHT(). A small example:

# create a string
a <- paste('left', 'right', sep = '')
a
# [1] "leftright"

我想生成b,一个等于a的前4个字母的字符串:

I would like to produce b, a string which is equal to the first 4 letters of a:

b
# [1] "left"

我该怎么办?

推荐答案

查看 ?substr

R> substr(a, 1, 4)
[1] "left"

这篇关于提取字符串的前(或最后)n 个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆