用向量中的 NA 替换空条目 [英] Replace empty entries with NA within a vector

查看:77
本文介绍了用向量中的 NA 替换空条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 替换向量中所有出现的 "".假设,

I would like to replace all occurrences of "" in a vector with <NA>. Suppose,

 V<- c("","Axe","Saw","")

然后替换后,

  V<- c("<NA>","Axe","Saw","<NA>")

使用 gsub 替换子字符串很容易,但要搜索 "" 我想我需要使用 /\"" 里面,但不知道怎么做.

Using gsub to replace a substring is easy but for searching "" I think I need to use / or \ inside "" but not sure how do do it.

推荐答案

有一个函数以您要执行的操作命名:replace().

There is a function named after exactly what you are trying to do: replace().

有了它,你可以:

> V<- c("","Axe","Saw","")
> V
[1] ""    "Axe" "Saw" ""   
> replace(V, V == "", NA)
[1] NA    "Axe" "Saw" NA 

这就像说用 NA"替换向量 'V' 中任何等于 '""' 的值.所以,这不是您问题标题的逐字转录,但非常接近:-)

That's like saying "replace in the vector 'V' any values equal to '""' with NA". So, it's not a word-for-word transcription of your question title, but it is pretty close :-)

这篇关于用向量中的 NA 替换空条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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