skan skan - 2 months ago 6
R Question

How to run a function inside data.table?

I've created an example

data.table


library(data.table)
set.seed(1)
siz <- 10
my <- data.table(
AA=c(rep(NA,siz-1),"11/11/2001"),
BB=sample(c("wrong", "11/11/2001"),siz, prob=c(1000000,1), replace=T),
CC=sample(siz),
DD=rep("11/11/2001",siz),
EE=rep("HELLO", siz)
)

my[2,AA:=1]


NA wrong 3 11/11/2001 HELLO
1 wrong 2 11/11/2001 HELLO
NA wrong 6 11/11/2001 HELLO
NA wrong 10 11/11/2001 HELLO
NA wrong 5 11/11/2001 HELLO
NA wrong 7 11/11/2001 HELLO
NA wrong 8 11/11/2001 HELLO
NA wrong 4 11/11/2001 HELLO
NA wrong 1 11/11/2001 HELLO
11/11/2001 wrong 9 11/11/2001 HELLO


If I run this code

patt <- "^\\d\\d?/\\d\\d?/\\d{4}$"
sapply(my, function(x) (grepl(patt,x )))


I get a table with
TRUE
whenever there is a date.

AA BB CC DD EE
[1,] FALSE FALSE FALSE TRUE FALSE
[2,] FALSE FALSE FALSE TRUE FALSE
[3,] FALSE FALSE FALSE TRUE FALSE
[4,] FALSE FALSE FALSE TRUE FALSE
[5,] FALSE FALSE FALSE TRUE FALSE
[6,] FALSE FALSE FALSE TRUE FALSE
[7,] FALSE FALSE FALSE TRUE FALSE
[8,] FALSE FALSE FALSE TRUE FALSE
[9,] FALSE FALSE FALSE TRUE FALSE
[10,] TRUE FALSE FALSE TRUE FALSE


But if I do it like this:

my[,lapply(.SD, grepl, patt)]


I just get this result:

AA BB CC DD EE
1: NA FALSE FALSE FALSE FALSE


Why?
How can I get the same result writing wverything inside the brackets?

Answer

We need to specify the pattern argument if we are not using anonymous function call

my[,lapply(.SD, grepl, pattern = patt)]

Or otherwise with an anonymous function call

my[,lapply(.SD, function(x) grepl(patt, x))]