修剪数据框中字符串的一部分 [英] Trim part of a string in dataframe

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

问题描述

如果我有这样的数据帧结构:

If I have a dataframe structure like that:

AA1_123.zip
BB2_456.txt
CCC_789.doc

我怎样才能把它改成这样:

How can I change it to this:

AA1
BB2
CCC

推荐答案

如果字符串开头都是一样的样式,下划线前三个字符,这样就可以了:

If the strings are all the same style at the start, three characters before the underline, this will work:

df1 <- structure(list(Col = c("AA1_123.zip", "BB2_456.txt", 
                              "CCC_789.doc"
)), .Names = "Col", class = "data.frame", row.names = c(NA, -3L))

> substr(df1$Col, 1, 3)
[1] "AA1" "BB2" "CCC"

这篇关于修剪数据框中字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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