[R] Using a function for rows over a subset of columns in data.table
Camilo Mora
cmora at Dal.Ca
Tue Oct 28 06:38:06 CET 2014
Hi everyone,
This may be a trivial solution but I have not been able to figure what is wrong with this code.
The basic premise of the code is to use data.table to append a new column to a data.table that indicates if the values per row at two other columns are within a 2d hull generated with the package alphahull.
I get the right results when I apply the function to a data.table with only the two columns to consider, But the results are off when I try to apply the same function selecting the two columns from a data.table with numerous columns. I think my problem is how the columns are selected for use in the function. Any help will be much appreciated.
library (alphahull)
library(data.table)
#creates a hull
DT=data.frame(x=c(0.25,0.25,0.75,0.75),y=c(0.25,0.75,0.75,0.25))
Hull <- ahull(DT, alpha = 0.5)
#applying the "inahull" function to a datatable with just the two colunms
TwoColumn<- data.table(x=c(0.25,0.5,0.5,0.5,0),y=c(0.5,0.5,0.5,.5,0))
TwoColumn[, INHULL := apply(TwoColumn, 1, function(x) inahull(Hull, x))]
#the results are correct
#now if I apply the same function to a selection of two columns in a data.table with several columns, the codes dies not work
ThreColumn<- data.table(w=c(1,2,3,4,5), z=c(1,2,3,4,5),x=c(0.25,0.5,0.5,0.5,0),y=c(0.5,0.5,0.5,.5,0))
ThreColumn[, INHULL := lapply(.SD, function(x) inahull(Hull, x)), .SDcols=c(x,y)]
[[alternative HTML version deleted]]
More information about the R-help
mailing list