[datatable-help] Extracting specific values from a matrix

Mike.Gahan michael.gahan at gmail.com
Thu Aug 21 15:42:20 CEST 2014


Here is one way to approach this problem (there are lots of ways)

#Convert to model matrix to account for factor variables
iris <- model.matrix(~.-1,data=iris)

#Convert to data.table
iris <- data.table(iris)
candidates <- names(iris)

#Setup interaction variables for every combination
all.interactions <- as.data.table(t(combn(candidates,2)))
all.interactions[ , interaction := paste(V1,V2,sep="__")]
all.interactions[ , multiplier := paste(V1,V2,sep="*")]

#Add interactions to data
for (x in 1:nrow(all.interactions))
  iris[ , paste0(all.interactions$interaction[x]) :=
eval(parse(text=all.interactions$multiplier[x]))]



--
View this message in context: http://r.789695.n4.nabble.com/Extracting-specific-values-from-a-matrix-tp4695768p4695793.html
Sent from the datatable-help mailing list archive at Nabble.com.


More information about the datatable-help mailing list