简体   繁体   English

R function 用于删除列中的前 4 个字符?

[英]R function for removing first 4 characters in a column?

I have a data frame (Data) and I have created a new column with the first four characters of each vector in a column (Details) using substr.我有一个数据框(数据),并且我使用 substr 创建了一个新列,其中每个向量的前四个字符在一列(详细信息)中。

Here is the code:这是代码:

Data$Years = substr(Data$Details, 1, 5)

How would I use substr (or another function) to remove the same first four characters from the original (Details) column?我将如何使用substr (或其他函数)从原始(详细信息)列中删除相同的前四个字符?

We can specify the first (in substring ) or start in substr as the 5th character and last (not needed in substring as it is by default last = 1000000L ) or stop in substr as the last character ( nchar )我们可以指定第first (在substring中)或start substr开始作为第 5 个字符和last (在substring中不需要,因为默认情况下last = 1000000L )或在substrstop作为最后一个字符( nchar

substring(Data$Details, 5)

Or with substr或使用substr

substr(Data$Details, 5, nchar(Data$Details))

Or using a regex to match any character ( . - repeated 4 times) from the start ( ^ ) of the string and replace with blank ( "" )或使用正则表达式从字符串的开头( ^ )匹配任何字符( . - 重复 4 次)并替换为空白( ""

sub("^....", "", Data$Details)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM