使用R中的gsub替换数据帧列中的特定值 [英] Replacing the specific values in columns of data frame using gsub in R

查看:132
本文介绍了使用R中的gsub替换数据帧列中的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的data.frame如下

I have data.frame as follows

> df
ID      Value
A_001   DEL-1:7:35-8_1 
A_002   INS-4l:5_74:d
B_023   0 
C_891   2
D_787   8
E_865   DEL-3:65:1s:b

我想将以DEL和INS开头的值列中的所有值全部替换为空.我的意思是我想得到如下输出

I would like replace all the values in the column Value that starts with DEL and INS with nothing. I mean i would like get the output as follows

> df
ID      Value
A_001   
A_002   
B_023   0 
C_891   2
D_787   8
E_865   

我尝试使用下面的代码在R中使用gsub来实现此目的,但是它不起作用

I tried to achieve this using gsub in R using following code but it didnt work

gsub(pattern="(^([DEL|INS]*)",replacement="",df)

任何人都可以指导我如何实现所需的输出.

Could anyone guide me how to achieve the desired output.

谢谢.

推荐答案

只需删除字符类,然后在该组旁边添加.*.仅sub可以完成这项工作.

Just remove the character class and add .* next to that group. sub alone would do this job.

df$value <- sub("^(DEL|INS).*", "", df$value)

在字符类中,每个字符都将被视为不是整个字符串.因此[DEL]将匹配给定列表中的单个字符,它可能是DEL.

Inside a character class, each char would be treated speartely not as a whole string. So [DEL] would match a single character from the given list, it may be D or E or L .

这篇关于使用R中的gsub替换数据帧列中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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