将NA更改为""在R中的向量中

时间:2017-10-11 08:03:16

标签: r

嗨我有一个df,如下所示

 a = c("AP_", "AP_", NA, "AP_", "AP_", "HP_", "AP_", "AP_") 
 b = c(20, 30, 40, 10, 20, 10, NA, 12) 
 c = c("A", "B", "C", "D", "E", "", "G", "H") 
 a_output = c("AP_", "AP_", "", "AP_", "AP_", "HP_", "AP_", "AP_") 

 df = data.frame(a, b, c,a_output)

如何获得" a_output"作为结果,通过将" a"中的NA转换为NULL柱

I have tried with grepl as below

  df$a_output = df[is.na(df$a)] <- "" 

以上声明中的任何修改 提前致谢

4 个答案:

答案 0 :(得分:3)

我们需要使用is.na

df$a_output <- replace(as.character(df$a), is.na(df$a), "")

另外,如果我们查看&#39; a&#39;的class列,factor,因此''列应该是factor列中的一个级别

levels(df$a) <- c(levels(df$a), "")
df$a[is.na(df$a)] <- ""

或我们转换为characterreplace,如第一种方法

答案 1 :(得分:2)

使用namespace ConsoleApplication1 { public class Program { static void Main(string[] args) { ImagineThisIsInTestProject(); Console.ReadLine(); Console.WriteLine("Started"); var cls = new Program(); cls.MyProdMethod(); Console.WriteLine("Finished"); Console.ReadLine(); } public string Status { get; private set; } public void MyProdMethod() { // I have more logic in my real method, serialization, handling errors Console.WriteLine("In my method"); var task = Task.Factory.StartNew(() => { Thread.Sleep(2000); Console.WriteLine("Setting status"); Status = "Yes I set it"; }, CancellationToken.None); Console.WriteLine("Finishing my method"); } public static void ImagineThisIsInTestProject() { Console.WriteLine("TEST started"); var sut = new Program(); var task = new Task(() => sut.MyProdMethod()); task.RunSynchronously(); // I would assume this will wait until the task is finished? // here my assert if status is set Console.WriteLine($"Status is - {sut.Status}"); Console.WriteLine("TEST finished"); } } } 可以实现另一种类似于akrun TEST started In my method Finishing my method Status is - TEST finished Setting status Started In my method Finishing my method Finished Setting status 答案的解决方案:

replace()

我不知道它如何与速度进行比较,但它有效:

ifelse()

您可以使用

将其转换为df$a_output <- ifelse(is.na(df$a), "", as.character(df$a))
> df
     a  b c
1  AP_ 20 A
2  AP_ 30 B
3 <NA> 40 C
4  AP_ 10 D
5  AP_ 20 E
6  HP_ 10  
7  AP_ NA G
8  AP_ 12 H

df$a_output <- ifelse(is.na(df$a), "", as.character(df$a))

> df
     a  b c a_output
1  AP_ 20 A      AP_
2  AP_ 30 B      AP_
3 <NA> 40 C         
4  AP_ 10 D      AP_
5  AP_ 20 E      AP_
6  HP_ 10        HP_
7  AP_ NA G      AP_
8  AP_ 12 H      AP_

如果你需要它。

答案 2 :(得分:1)

如果您不希望a,c,a_output是因子变量,解决方案可能如下所示:

df = data.frame(a, b, c,a_output,stringsAsFactors = F)

df$a_output[is.na(df$a)]  <- ""

答案 3 :(得分:1)

另一个解决方案:使用两个步骤,但使用which()函数,我认为这有助于提高可读性。

a_output<-a

a_output[which(is.na(a_output))]<-""  ## which is na, assign ""