How to store a "complex" data structure in R (not "complex numbers") -
i need train, store, , use list/array/whatever of several ksvm svm models, once set of sensor readings, can call predict()
on each of models in turn. want store these models , metadata tham in sort of data structure, i'm not familiar r, , getting handle on data structures has been challenge. familiarity c++, c, , c#.
i envision sort of array or list contains both ksvm models metadata them. (the metadata necessary, among other things, knowing how select & organize input data presented each model when call predict()
on it.)
the data want store in data structure includes following each entry of data structure:
- the ksvm model itself
- a character string saying trained model & when trained
- an array of numbers indicating sensors' data should presented model
- a single number between 1 , 100 represents how i, trainer, trust model
- some "other stuff"
so in tinkering how this, tried following....
first tried thought simple & crude, hoping build on later if worked: (list of (list of different data types))...
> > uname = sys.getenv("username", unset="unknown_user") > cname = sys.getenv("computername", unset="unknown_computer") > trainedat = paste("trained at", sys.time(), "by", uname, "on", cname) > trainedat [1] "trained @ 2015-04-22 20:54:54 mminich on mminich1" > sensorstouse = c(12,14,15,16,24,26) > sensorstouse [1] 12 14 15 16 24 26 > trustfactor = 88 > > testmodels = list() > testmodels[1] = list(trainedat, sensorstouse, trustfactor) warning message: in testmodels[1] = list(trainedat, sensorstouse, trustfactor) : number of items replace not multiple of replacement length > > testmodels [[1]] [1] "trained @ 2015-04-22 20:54:54 mminich on mminich1" >
...wha? did think trying replace? trying populate element 1 of testmodels. later add element [2], [3], etc... didn't work , don't know why. maybe need define testmodels list of lists right front...
> testmodels = list(list()) > testmodels[1] = list(trainedat, sensorstouse, trustfactor) warning message: in testmodels[1] = list(trainedat, sensorstouse, trustfactor) : number of items replace not multiple of replacement length >
hmm. no workie either. let's try else...
> testmodels = list(list()) > testmodels[1][1] = list(trainedat, sensorstouse, trustfactor) warning message: in testmodels[1][1] = list(trainedat, sensorstouse, trustfactor) : number of items replace not multiple of replacement length >
drat. still no workie.
please clue me in on how can this. , i'd able access fields of data structure name, perhaps along lines of...
> print(testmodels[1]["trainedat"])
thank much!
you close. avoid warning, shouldn't use
testmodels[1] = list(trainedat, sensorstouse, trustfactor)
but instead
testmodels[[1]] = list(trainedat, sensorstouse, trustfactor)
to access list element use [[ ]]
. using [ ]
on list return list containing elements inside single brackets. warning shown because replacing list containing 1 element (because how created it) list containing 3 elements. wouldn't problem other elements:
testmodels[2] = list(trainedat, sensorstouse, trustfactor) # element did not exist, no replacement warning
to understand list subsetting better, take @ this:
item1 <- list("a", 1:10, c(t, f, t)) item2 <- list("b", 11:20, c(f, f, f)) mylist <- list(item1=item1, item2=item2) mylist[1] #this returns list containing item 1. #$item1 #note item name of container list #$item1[[1]] #[1] "a" # #$item1[[2]] # [1] 1 2 3 4 5 6 7 8 9 10 # #$item1[[3]] #[1] true false true # mylist[[1]] #this returns item1 #[[1]] #note same item1 #[1] "a" # #[[2]] # [1] 1 2 3 4 5 6 7 8 9 10 # #[[3]] #[1] true false true
to access list items name, name them when creating list:
mylist <- list(var1 = "a", var2 = 1:10, var3 = c(t, f, t)) mylist$var1 #or mylist[["var1"]] # [1] "a"
you can nest operators suggested. coud use
containerlist <- list(mylist) containerlist[[1]]$var1 #[1] "a"