简体   繁体   中英

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

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. How would I go about this? I feel like using a for loop might work but I'm unsure of the syntax required.

Thanks

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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