Home > OS >  Using strsplit with lapply
Using strsplit with lapply

Time:09-18

I have a question with using strsplit with laaply function. I'm using titanic dataset and wants to split name by ",","." and extract "Mr", "Mrs" and so on.

I tried this code and gives me error

lapply(data$Name, strsplit(data$Name, split = "[,.]")[[1]][2])

Error in get(as.character(FUN), mode = "function", envir = envir) : object ' Mr' of mode 'function' was not found

However, this code works

lapply(data$Name, function(x)strsplit(x, split = "[,.]")[[1]][2])

[[995]] [1] " Mr" ...

I don't know what's the difference between those two..

CodePudding user response:

The second argument of lapply needs to be a function. The expression strsplit(data$Name, split = "[,.]")[[1]][2] is not a function, it is the ouput of strplit() applied to data$Name with your addtional arguments.

In your second example, which does have a (anonymous) function as the second argument, it is clear what lapply needs to do with each element of the first argument.

Consider:

data <- list(1,2,3)

lapply(data, paste0(data, "_")) # Error, paste0(data, "_") is a vector, not a function
lapply(data, function(x) paste0(x, "_")) # Works
[[1]]
[1] "1_"

[[2]]
[1] "2_"

[[3]]
[1] "3_"
  • Related