在R中修剪数据,去掉“*" [英] Trimming data in R, getting rid of "*"

查看:28
本文介绍了在R中修剪数据,去掉“*"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据集:

I have a data set which looks like:

> data<-c( "IGHV1-2*02 F, or  IGHV1-2*03 F","IGHV3-23*01 F, or 
> IGHV3-23*04 F","IGHV2-70*01 F","IGHV7-4-1*01")

我想保留V1-2"的首次亮相例如并删除后面的任何内容(包括*").所以我尝试了以下方法:

I would like to keep the first appearance of "V1-2" for example and delete anything which follows (including the "*"). So I tried the following:

> data.substr<-substr(data,4,9)
> data.substr1<-gsub("*","",data.substr)

但我仍然无法摆脱*",可能是因为它用作占位符......有人有想法吗?

but I still cant get rid of the "*", probably because it serves as a placeholder... Does anyone have an idea?

推荐答案

gsub("[*].*$","",data)

* 放在方括号中,它将被视为字符,然后任何值 .* 直到字符串 $ 的末尾被删除.

put * in square brackets, it will be treated as character, then any value .* until the end of the string $ will be removed.

这篇关于在R中修剪数据,去掉“*"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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