Mapping key values between different size vectors
I am still new to scala and am having a rough time getting this functionality to work in my code.
I am trying to create a mapping of 24 hours from hour_vector and fill the "visitor" values whenever there is a visitor for the particular hour in the hour_visitors vector
val hour_visitors = Vector((10,100),(11,25),(16,200))
val hour_vector = Vector(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(a)._2).toMap.withDefaultValue(0)
This does not work as I am getting index out of bounds error (which makes sense).
However if I replace:
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(1)._2).toMap.withDefaultValue(0)
This will map every value from the 1st index of hour_visitor.
I can re-map the values from hour_index to a list but that defeats the idea, as I am losing the "hour" key in that instance and the values will be inserted incorrectly into the 24 hour vector.
How can I use standard library to map the values from 1 vector into another only when the keys exist in both vectors and if they are not present in 1 vector then output a value of 0.
In python I could just use a merge on 2 dataframes with all.x or all.y, this scala approach is still quite confusing for me.
Thank you
scala
add a comment |
I am still new to scala and am having a rough time getting this functionality to work in my code.
I am trying to create a mapping of 24 hours from hour_vector and fill the "visitor" values whenever there is a visitor for the particular hour in the hour_visitors vector
val hour_visitors = Vector((10,100),(11,25),(16,200))
val hour_vector = Vector(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(a)._2).toMap.withDefaultValue(0)
This does not work as I am getting index out of bounds error (which makes sense).
However if I replace:
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(1)._2).toMap.withDefaultValue(0)
This will map every value from the 1st index of hour_visitor.
I can re-map the values from hour_index to a list but that defeats the idea, as I am losing the "hour" key in that instance and the values will be inserted incorrectly into the 24 hour vector.
How can I use standard library to map the values from 1 vector into another only when the keys exist in both vectors and if they are not present in 1 vector then output a value of 0.
In python I could just use a merge on 2 dataframes with all.x or all.y, this scala approach is still quite confusing for me.
Thank you
scala
add a comment |
I am still new to scala and am having a rough time getting this functionality to work in my code.
I am trying to create a mapping of 24 hours from hour_vector and fill the "visitor" values whenever there is a visitor for the particular hour in the hour_visitors vector
val hour_visitors = Vector((10,100),(11,25),(16,200))
val hour_vector = Vector(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(a)._2).toMap.withDefaultValue(0)
This does not work as I am getting index out of bounds error (which makes sense).
However if I replace:
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(1)._2).toMap.withDefaultValue(0)
This will map every value from the 1st index of hour_visitor.
I can re-map the values from hour_index to a list but that defeats the idea, as I am losing the "hour" key in that instance and the values will be inserted incorrectly into the 24 hour vector.
How can I use standard library to map the values from 1 vector into another only when the keys exist in both vectors and if they are not present in 1 vector then output a value of 0.
In python I could just use a merge on 2 dataframes with all.x or all.y, this scala approach is still quite confusing for me.
Thank you
scala
I am still new to scala and am having a rough time getting this functionality to work in my code.
I am trying to create a mapping of 24 hours from hour_vector and fill the "visitor" values whenever there is a visitor for the particular hour in the hour_visitors vector
val hour_visitors = Vector((10,100),(11,25),(16,200))
val hour_vector = Vector(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(a)._2).toMap.withDefaultValue(0)
This does not work as I am getting index out of bounds error (which makes sense).
However if I replace:
val map_visitors_to_hours = hour_vector.map(a => a -> hour_visitors(1)._2).toMap.withDefaultValue(0)
This will map every value from the 1st index of hour_visitor.
I can re-map the values from hour_index to a list but that defeats the idea, as I am losing the "hour" key in that instance and the values will be inserted incorrectly into the 24 hour vector.
How can I use standard library to map the values from 1 vector into another only when the keys exist in both vectors and if they are not present in 1 vector then output a value of 0.
In python I could just use a merge on 2 dataframes with all.x or all.y, this scala approach is still quite confusing for me.
Thank you
scala
scala
asked Dec 28 '18 at 3:01
user3674993
2516
2516
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
One approach would be to make hour_visitors a Map and use getOrElse to fetch values for the hours in hour_vector as shown below
val map_hour_visitors = hour_visitors.toMap
val map_visitors_to_hours = hour_vector.map( h =>
(h, map_hour_visitors.getOrElse(h, 0))
).toMap
// map_visitors_to_hours: scala.collection.immutable.Map[Int,Int] = Map(
// 0 -> 0, 5 -> 0, 10 -> 100, 14 -> 0, 20 -> 0, 1 -> 0, 6 -> 0, 21 -> 0,
// 9 -> 0, 13 -> 0, 2 -> 0, 17 -> 0, 22 -> 0, 12 -> 0, 7 -> 0, 3 -> 0,
// 18 -> 0, 16 -> 200, 11 -> 25, 23 -> 0, 8 -> 0, 19 -> 0, 4 -> 0, 15 -> 0
// )
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
1
@user3674993, from performance point of view, I'd say the firsttoMapis well worth it as you getO(1)inMaplookup. As to the othertoMap, it can be skipped if your data specs allow the result to be inVector[(Int, Int)].
– Leo C
Dec 28 '18 at 3:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953207%2fmapping-key-values-between-different-size-vectors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One approach would be to make hour_visitors a Map and use getOrElse to fetch values for the hours in hour_vector as shown below
val map_hour_visitors = hour_visitors.toMap
val map_visitors_to_hours = hour_vector.map( h =>
(h, map_hour_visitors.getOrElse(h, 0))
).toMap
// map_visitors_to_hours: scala.collection.immutable.Map[Int,Int] = Map(
// 0 -> 0, 5 -> 0, 10 -> 100, 14 -> 0, 20 -> 0, 1 -> 0, 6 -> 0, 21 -> 0,
// 9 -> 0, 13 -> 0, 2 -> 0, 17 -> 0, 22 -> 0, 12 -> 0, 7 -> 0, 3 -> 0,
// 18 -> 0, 16 -> 200, 11 -> 25, 23 -> 0, 8 -> 0, 19 -> 0, 4 -> 0, 15 -> 0
// )
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
1
@user3674993, from performance point of view, I'd say the firsttoMapis well worth it as you getO(1)inMaplookup. As to the othertoMap, it can be skipped if your data specs allow the result to be inVector[(Int, Int)].
– Leo C
Dec 28 '18 at 3:54
add a comment |
One approach would be to make hour_visitors a Map and use getOrElse to fetch values for the hours in hour_vector as shown below
val map_hour_visitors = hour_visitors.toMap
val map_visitors_to_hours = hour_vector.map( h =>
(h, map_hour_visitors.getOrElse(h, 0))
).toMap
// map_visitors_to_hours: scala.collection.immutable.Map[Int,Int] = Map(
// 0 -> 0, 5 -> 0, 10 -> 100, 14 -> 0, 20 -> 0, 1 -> 0, 6 -> 0, 21 -> 0,
// 9 -> 0, 13 -> 0, 2 -> 0, 17 -> 0, 22 -> 0, 12 -> 0, 7 -> 0, 3 -> 0,
// 18 -> 0, 16 -> 200, 11 -> 25, 23 -> 0, 8 -> 0, 19 -> 0, 4 -> 0, 15 -> 0
// )
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
1
@user3674993, from performance point of view, I'd say the firsttoMapis well worth it as you getO(1)inMaplookup. As to the othertoMap, it can be skipped if your data specs allow the result to be inVector[(Int, Int)].
– Leo C
Dec 28 '18 at 3:54
add a comment |
One approach would be to make hour_visitors a Map and use getOrElse to fetch values for the hours in hour_vector as shown below
val map_hour_visitors = hour_visitors.toMap
val map_visitors_to_hours = hour_vector.map( h =>
(h, map_hour_visitors.getOrElse(h, 0))
).toMap
// map_visitors_to_hours: scala.collection.immutable.Map[Int,Int] = Map(
// 0 -> 0, 5 -> 0, 10 -> 100, 14 -> 0, 20 -> 0, 1 -> 0, 6 -> 0, 21 -> 0,
// 9 -> 0, 13 -> 0, 2 -> 0, 17 -> 0, 22 -> 0, 12 -> 0, 7 -> 0, 3 -> 0,
// 18 -> 0, 16 -> 200, 11 -> 25, 23 -> 0, 8 -> 0, 19 -> 0, 4 -> 0, 15 -> 0
// )
One approach would be to make hour_visitors a Map and use getOrElse to fetch values for the hours in hour_vector as shown below
val map_hour_visitors = hour_visitors.toMap
val map_visitors_to_hours = hour_vector.map( h =>
(h, map_hour_visitors.getOrElse(h, 0))
).toMap
// map_visitors_to_hours: scala.collection.immutable.Map[Int,Int] = Map(
// 0 -> 0, 5 -> 0, 10 -> 100, 14 -> 0, 20 -> 0, 1 -> 0, 6 -> 0, 21 -> 0,
// 9 -> 0, 13 -> 0, 2 -> 0, 17 -> 0, 22 -> 0, 12 -> 0, 7 -> 0, 3 -> 0,
// 18 -> 0, 16 -> 200, 11 -> 25, 23 -> 0, 8 -> 0, 19 -> 0, 4 -> 0, 15 -> 0
// )
edited Dec 28 '18 at 3:26
answered Dec 28 '18 at 3:20
Leo C
10.3k2617
10.3k2617
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
1
@user3674993, from performance point of view, I'd say the firsttoMapis well worth it as you getO(1)inMaplookup. As to the othertoMap, it can be skipped if your data specs allow the result to be inVector[(Int, Int)].
– Leo C
Dec 28 '18 at 3:54
add a comment |
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
1
@user3674993, from performance point of view, I'd say the firsttoMapis well worth it as you getO(1)inMaplookup. As to the othertoMap, it can be skipped if your data specs allow the result to be inVector[(Int, Int)].
– Leo C
Dec 28 '18 at 3:54
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
This is it! I can follow the logic but working this out on myself would be quite difficult, just seems like so many toMap calls to get something so trivial to work properly (perhaps it isnt as trivial as I think it is)
– user3674993
Dec 28 '18 at 3:34
1
1
@user3674993, from performance point of view, I'd say the first
toMap is well worth it as you get O(1) in Map lookup. As to the other toMap, it can be skipped if your data specs allow the result to be in Vector[(Int, Int)].– Leo C
Dec 28 '18 at 3:54
@user3674993, from performance point of view, I'd say the first
toMap is well worth it as you get O(1) in Map lookup. As to the other toMap, it can be skipped if your data specs allow the result to be in Vector[(Int, Int)].– Leo C
Dec 28 '18 at 3:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953207%2fmapping-key-values-between-different-size-vectors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown