Reputation: 2747
I have a pandas DataFrame df with the following content:
Serial N voltage
B 12
B 10
C 12
C 40
. .
AB 10
AB 13
I would like to build a hashmap from this dataFrame where the key is the serial number (unique) and the value is a list corresponding the voltage of that serial number. For example, I would like to have something like
df[[B]]=[12,10] or df[[c]]=[12,40].
How can I do in R?
Upvotes: 0
Views: 2312
Reputation: 663
df <- data.frame(s=sample(LETTERS[1:2], 5, T), v=sample(10:40, 5),
stringsAsFactors=FALSE)
h <- aggregate(v~s, data=df, FUN=c)
setNames(h[,2], h[,1])
> df
s v
1 A 29
2 B 22
3 A 33
4 B 26
5 A 37
> setNames(h[,2], h[,1])
$A
[1] 29 33 37
$B
[1] 22 26
Upvotes: 0
Reputation: 18612
Here's another base R approach:
Hash <- sapply(unique(Df$Key), function(x) {
Df[Df$Key == x, 2]
}, simplify = FALSE)
Df[Df$Key %in% c("A", "B"),]
# Key Value
# 1 A 61
# 2 A 52
# 3 B 46
# 4 B 53
Hash[["A"]]
#[1] 61 52]
Hash[["B"]]
#[1] 46 53
Data:
Df <- data.frame(
Key = sort(rep(LETTERS, 2)),
Value = rpois(52, 50)
)
Upvotes: 2
Reputation: 78832
You just need to do some basic transformations (all in base R):
set.seed(1492)
dat <- data.frame(serial_n=sample(c("A", "B", "C", "AB", "AC", "BC"), 100, replace=TRUE),
voltage=sample(10:30, 100, replace=TRUE),
stringsAsFactors=FALSE)
head(dat)
## serial_n voltage
## 1 B 20
## 2 B 21
## 3 B 18
## 4 A 19
## 5 A 11
## 6 A 16
unclass(by(dat, dat$serial_n, function(x) {
tmp <- x$voltage
setNames(tmp, x$serial_n[1])
tmp
})) -> df
df[["A"]]
## [1] 19 11 16 23 28 13 20 23 27 27 28 11 27 12 25 26 19
df[["AB"]]
## [1] 20 28 29 16 10 28 18 19 27 20 27 26 15 20 28 21 28 21 24 19 12
Upvotes: 2