[datatable-help] Replace a character in one data table column

jim holtman jholtman at gmail.com
Tue Jul 29 19:08:20 CEST 2014


Is this what you want:

> require(data.table)
> DT <- data.table(id=c(1,1,4,4,4),
+
+
date=as.character(c("1997-04-26","missing","1998-08-25","missing","1998-11-07")))
> DT
   id       date
1:  1 1997-04-26
2:  1    missing
3:  4 1998-08-25
4:  4    missing
5:  4 1998-11-07
> DT[date == "missing", date := '2002-06-30']
> DT
   id       date
1:  1 1997-04-26
2:  1 2002-06-30
3:  4 1998-08-25
4:  4 2002-06-30
5:  4 1998-11-07
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Tue, Jul 29, 2014 at 12:28 PM, Frank S. <f_j_rod at hotmail.com> wrote:
> Hi everyone,
>
>
>
> I would want to replace only the "missing" characters in date column by
> "2002-06-30" character (I will make the change  to date format in future):
>
>
>
> DT <- data.table(id=c(1,1,4,4,4),
>
>
> date=as.character(c("1997-04-26","missing","1998-08-25","missing","1998-11-07")))
> DT
>
>
>
>    id       date
> 1:  1 1997-04-26
> 2:  1         NA
> 3:  4 1998-08-25
> 4:  4         NA
> 5:  4 1998-11-07
>
>
>
>
>
> DT[,list(id, date=if(variable==NA) {"2002-06-30"} else date)]
>
>
>
> But I get an error message.
>
>
>
> Is it posible to do it under data table format?
>
>
>
> Many thanks to all the data.table help  members!!
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help


More information about the datatable-help mailing list