简体   繁体   English

R 空字符串替换

[英]R empty string replacement

I should convert 'N/A' and 'not known' to a blank string “”我应该将 'N/A' 和 'not known' 转换为空白字符串“”

   Var1     Freq
1  N/A       650
2  NONE      264
3  NOT KNOWN  58

to be like this像这样

Var1  Freq
1      650
2      264
3      58

and

"" <- new_building[grepl("N/A|NOT|KNOWN", new_building$Var1),]

I used this but this is not working我用过这个,但这不起作用

> string<-'asdaN/A'
> string2<-gsub(string,pattern='N/A',replacement='')
> string2
 [1] "asda"

do you mean this?你是这个意思吗?

Edit: following updated q:编辑:以下更新q:

string<-c('Var1','Freq', '1','N/A','650','2', 'NONE','264', '3', 'NOT KNOWN', '58')
string[!string%in%'N/A']

how about this:这个怎么样:

df_ <- data.frame(Var1 = c('N/A', 'NONE', 'NOT KNOWN'), Freq = c(650, 264, 58))

df_$Var1 <- gsub("N/A|NONE|NOT KNOWN", "", df_$Var1)
df_

#  Var1 Freq
#1       650
#2       264
#3        58

your assignment to "" ( "" <- ... ) is very wrong;你对"" ( "" <- ... ) 的分配是非常错误的; i am not sure what your logic is here.我不确定你的逻辑是什么。 the correct approach is to substitute strings satisfying your criteria with empty strings "" using gsub .正确的方法是使用gsub用空字符串""替换满足您条件的字符串。

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

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