如何从列名中删除第一组数字?
我有列名称,例如它们看起来像20819830_R1AR_U_STATIONARY
和2081974_F8AR_U
。我试图摆脱列名中的第一组数字。我尝试使用此代码,
names(df)[1:2] <- gsub("^.*_","",names(df[,c(1:2)]))
但是当我使用此代码时,列名称转换为stastary
和u
。我可以看到该代码将所有内容删除,直到最后一个_
如何更改代码,以便将所有内容删除到第一个_
。
I have column names, for example they look like this 20819830_r1ar_u_stationary
and 2081974_f8ar_u
. I am trying to get rid of the first set of numbers in the column names. I tried using this code
names(df)[1:2] <- gsub("^.*_","",names(df[,c(1:2)]))
but when I use this, the column names turn to stationary
and u
. I can see the code is removing everything up until the last _
how do I change the code so that it removes everything up until the first _
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Stringr
的另一个选项。str_remove
将删除下面的第一组数字:Another option using
stringr
.str_remove
will remove the first set of digits that are followed by an underscore:与其匹配
。*
- 一个或多个字符作为。
与任何字符匹配,它应该是一个或多个数字(\\ d+
)字符串的开始(^
)Instead of matching
.*
- one or more characters as.
matches any characters, it should be one or more digits (\\d+
) from the start (^
) of the string