Is it possible to store a numeric vector in the names variable of a list? ie.
x <- c(1.2,3.4,5.9)
alist <- list()
alist[[x]]$somevar <- 2
I know I can store it as a vector within the list element, but I thought it would be faster to move through and find the element of the list I want (or add if needed) if the numeric vector is the name of the list element itself...
EDIT:
I have included a snippit of the code in context below, apologies for the change in nomenclature. In brief, I am working on a clustering problem, the dataset is too large to directly do the distance calculation on, my solution was to create bins for each dimension of the data and find the nearest bin for each observation in the original data. Of course, I cannot make a complete permutation matrix since this would be larger than the original data itself. Therefore, I have opted to find the nearest bin for each dimension individually and add it to a vector, temp.bin
, which ideally would become the name of the list element in which the rowname of the original observation would be stored. I was hoping that this would simplify searching for and adding bins to the list.
I also realise that the distance calculation part is likely wrong - this is still very much a prototype.
binlist <- list()
for(i in 1:nrow(data)) # iterate through all data points
{
# for each datapoint make a container for the nearest bin found
temp.bin <- vector(length = length(markers))
names(temp.bin) <- markers
for(j in markers) # and dimensions
{
# find the nearest bin for marker j
if(dist == "eucl")
{
dists <- apply(X=bin.mat, MARGIN = 1, FUN= function(x,y) {sqrt(sum((x-y)^2))}, y=data[i,j])
temp.bin[j] <- bin.mat[which(dists == min(dists)),j]
}
}
### I realise this part doesn't work
binlist[[temp.bin]] <- append(binlist[[temp.bin]], values = i)
The closest answer so far is John Coleman.