How to replace the entire data.table line with NA?

It seems like it should be easy, but I can't figure it out.

>d=data.table(x=1:5,y=11:15,z=letters[1:5])
>d
   x  y z
1: 1 11 a
2: 2 12 b
3: 3 13 c
4: 4 14 d
5: 5 15 e

Now I decided that line 3 is bad data. I want all of them to be installed in NA.

d[3,]<-NA

Warning message: B [<-.data.table( *tmp*, 3, value = NA): Forced the “logical” RHS to “character” to match the type of column. Either first change the target column to “logical” (by creating a new “logical” length of vector 5 (threads of the entire table) and assign it; that is, “replace”), or force RHS to “character” (for example, 1L, NA_ [real | integer] _, as. * Etc.), to make your intention clear for speed. Or set the column type correctly when you create the table and stick to it please.

, , .

> d
    x  y  z
1:  1 11  a
2:  2 12  b
3: NA NA NA
4:  4 14  d
5:  5 15  e

data.frame, , . , . ?

+5
4

.

DT[rownum, (names(DT)) := lapply(.SD, function(x) {  .x <- x[1]; is.na(.x) <- 1L; .x})]

, ,

DT[rownum, (names(DT)) := lapply(.SD[1,], function(x) { is.na(x) <- 1L; x})]

NA ( )

, , DT , rownum .

( Roland, .

DT[rownum, (names(DT)) := .SD[NA]]
+8

NA:

d[3,] <- list(NA_integer_, NA_integer_, NA_character_)

:

d[3,] <- d[3,lapply(.SD,function(x) x[NA])]
+7

?set?

> d=data.table(x=1:5,y=11:15,z=letters[1:5])
> set(d, 3L, 1:3, NA_character_)
> d
    x  y  z
1:  1 11  a
2:  2 12  b
3: NA NA NA
4:  4 14  d
5:  5 15  e
> str(d)
Classes ‘data.table’ and 'data.frame':  5 obs. of  3 variables:
 $ x: int  1 2 NA 4 5
 $ y: int  11 12 NA 14 15
 $ z: chr  "a" "b" NA "d" ...
 - attr(*, ".internal.selfref")=<externalptr> 

:

> d=data.table(x=1:5,y=11:15,z=letters[1:5])
> d[3] <- NA_character_
> str(d)
Classes ‘data.table’ and 'data.frame':  5 obs. of  3 variables:
 $ x: int  1 2 NA 4 5
 $ y: int  11 12 NA 14 15
 $ z: chr  "a" "b" NA "d" ...
 - attr(*, ".internal.selfref")=<externalptr> 

[ ] set() - , @mnel :

DT[rownum, names(DT) := .SD[NA]]

set ( , ). , ( double integer), , RHS.

if( (isReal(RHS) && (TYPEOF(targetcol)==INTSXP || isLogical(targetcol))) ||
    (TYPEOF(RHS)==INTSXP && isLogical(targetcol)) ||
    (isString(targetcol))) {
    if (isReal(RHS)) s3="; may have truncated precision"; else s3="";
    warning("Coerced '%s' RHS to '%s' to match the column type%s. ... <s3> ...
}

assign.c :
https://r-forge.r-project.org/scm/viewvc.php/pkg/src/assign.c?view=markup&root=datatable

, :

FR # 2551 Singleton: = RHS ,

.

In general, where it data.tablecautiously warns you of potential problems or inefficiencies, in this case, when you want to install a set of columns of different types, wrap with suppressWarnings()- this is another way.

+3
source

Here is what I am doing now. OK, I think, but still a little uncomfortable.

na_datatable_row<-function(dtrow){
  #take a row of data.table and return a row of the same table but 
  #with all values set tp NA
  #DT[rownum,]<-NA works but throws an annoying warning 
  #so instead, do DT[rownum,]<-na_datatable_row(DT[anyrow,]) 
  #this preserves the right types
  row=data.frame(dtrow)
  row[1,]<-NA
  return(data.table(row))
}
0
source

All Articles