在 R 中将字符串修剪为特定数量的字符 [英] Trim a string to a specific number of characters in R

查看:23
本文介绍了在 R 中将字符串修剪为特定数量的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符向量修剪为每个元素的前五个字符.在此示例中,我想将字符串中的每个数字修剪为前五个字符.我相信一定有一种简单的方法可以做到这一点.

I would like to trim a character vector to the first five characters in each element. In this example I would like to trim each number in string to the first five characters. I am sure there must be an easy way to do this.

string<-
c("3243423",
"23423",
"34243234",
"2342",
"32544532",
"85678657") 

我想要一个向量

c("32434",
"23423",
"34243",
"2342",
"32544",
"85678")

推荐答案

想通了.希望这会有所帮助.

Figured it out. Hope this is helpful.

strtrim(string, 5)

这篇关于在 R 中将字符串修剪为特定数量的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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