简体   繁体   English

是否有用于删除列中特定数据片段的 R 函数?

[英]Is there an R function for removing a specific snippet of the data in a column?

So I have a data frame that includes a column like this: image所以我有一个包含这样一列的数据框: image

And I would like to remove the operator as well as the numbers to the right of it, ie so the first entry would just say 51.81 rather than 51.81 - 11.19.我想删除运算符以及它右侧的数字,即第一个条目只会说 51.81 而不是 51.81 - 11.19。 How would I go about this?我该怎么办? I feel like using a for loop might work but I'm unsure of the syntax required.我觉得使用 for 循环可能有效,但我不确定所需的语法。

Thanks谢谢

我们可以使用sub来匹配零个或多个空格 ( \\\\s* ) 后跟-+等字符,并替换为空白 ( "" )

df1$xG <- as.numeric(sub("\\s*[-+]+.*", "", df1$xG))

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

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