Skip to content

Instantly share code, notes, and snippets.

@caseykneale
Created May 16, 2020 12:45
Show Gist options
  • Save caseykneale/bb69f11df170fa2ee72078f0f378ef24 to your computer and use it in GitHub Desktop.
Save caseykneale/bb69f11df170fa2ee72078f0f378ef24 to your computer and use it in GitHub Desktop.
Helping Kasemiir Alice with Tensors
kasemiir <- as.data.frame(read.csv2("/mydata2.csv"))
#i batches(BatchID), j variables and k time(Time)
batch_part <- split(kasemiir, f=kasemiir$BatchID)
#now we have a list of dataframes split over batchid
#z[[2]] #is batch2 etc...
#make sure each subdf is ordered by time and convert to matrix
for( subdf in batch_part){
subdf <- data.matrix( subdf[order("Time")] )
}
dimens = dim(batch_part[[2]])
batches = length( batch_part )
print(dimens)#dimens of 895(time) x 46(vars)
print(batches)#12 batches
ThreeTensor <- array( batch_part , dim = c( dimens[1] , dimens[2] , batches ) )
dim(ThreeTensor)
library(rTensor)# https://rdrr.io/cran/rTensor/man/unfold-methods.html
UnfoldedTensor <- unfold(as.tensor(ThreeTensor), 1, c(2,3))
UnfoldedMatrix <- as.matrix(UnfoldedTensor@data)
dim(UnfoldedMatrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment