Reputation: 740
I have a list of indexes in 3 dimensions;
> head(TW_idx, n = 4)
[[1]]
[1] 1 1 135
[[2]]
[1] 1 2 96
[[3]]
[1] 1 3 120
[[4]]
[1] 1 1 135
Each index corresponds to a value of 100. However, one index can occur more than one times in TW_idx (here in list 1 and 4) and for each occurence of the index, the value increases linearly (so if one index is present 3 times, the value of that index is 300). I want to find a way to store the index with its corresponding value.
My thought is that we create a dataframe which would look smth like
> df
idx value
1,1,135 200
1,2,96 100
1,3,120 100
for the 4 values above but I am not sure how to create it. Even better would be if I could get an output where the indexes are just the same list without duplicates (i.e. TW_idx <- unique(TW_idx) ) and the values are stored in a list/numeric list of the same length where each element describes the value for the corresponding element in the new (without duplicates) TW_idx list. But if it is simpler to get the dataframe, I would be very happy with that.
Thanks in advance
Upvotes: 1
Views: 51
Reputation: 70653
Is this enough? Basically I create a character variable from each list element. It is assumed that they are consistently written down. It is trivial to count the number of times each combination occurs. Right now the result is in raw count but there's really nothing stopping you to multiply this by 100.
x <- list(c(1, 1, 135),
c(1, 2, 96),
c(1, 3, 120),
c(1, 1, 135))
out <- as.data.frame(table(sapply(x, FUN = paste, collapse = ",")))
out
Var1 Freq
1 1,1,135 2
2 1,2,96 1
3 1,3,120 1
If you need the data in a list form, you can split it row-wise.
split(out, 1:nrow(out))
$`1`
Var1 Freq
1 1,1,135 2
$`2`
Var1 Freq
2 1,2,96 1
$`3`
Var1 Freq
3 1,3,120 1
Upvotes: 5