Dataframe: Divide each group by a vector corresponding to each group in R?
I have a data frame like this:
df1 <- structure(list(user_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), param_a = c(123,
2.3, -9, 1, -0.03333, 4, -41, -12, 0.89)), .Names = c("user_id",
"param_a"), row.names = c(NA, -9L), class = c("tbl_df", "tbl",
"data.frame"))
and another dataframe
of vectors:
df2 <- structure(list(user_id = c(1, 2, 3), param_b = c(34, 12, -0.89
)), .Names = c("user_id", "param_b"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
Now I want to divide each group in df1
by corresponding value in df2
:
For example for a group of user 1 divide each row by param_b
first vector:
user_id param_a
1 123/34
1 2.3/34
1 -9/34
2 1/12
2 -0.03333/12
2 4/12
....
for user 2 divide each row by param_b
second vector.
Please advise how can I divide a grouped by user dataframe
by a vector per each group?
P.S
If I have df1 extended to param_a, param_k, param_p
and df2 extended accordingly with param_b, param_l, param_r
How can I perform this kind of operation? @nicola suggested a very nice solution but I want to extend it.
r dataframe datatable
add a comment |
I have a data frame like this:
df1 <- structure(list(user_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), param_a = c(123,
2.3, -9, 1, -0.03333, 4, -41, -12, 0.89)), .Names = c("user_id",
"param_a"), row.names = c(NA, -9L), class = c("tbl_df", "tbl",
"data.frame"))
and another dataframe
of vectors:
df2 <- structure(list(user_id = c(1, 2, 3), param_b = c(34, 12, -0.89
)), .Names = c("user_id", "param_b"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
Now I want to divide each group in df1
by corresponding value in df2
:
For example for a group of user 1 divide each row by param_b
first vector:
user_id param_a
1 123/34
1 2.3/34
1 -9/34
2 1/12
2 -0.03333/12
2 4/12
....
for user 2 divide each row by param_b
second vector.
Please advise how can I divide a grouped by user dataframe
by a vector per each group?
P.S
If I have df1 extended to param_a, param_k, param_p
and df2 extended accordingly with param_b, param_l, param_r
How can I perform this kind of operation? @nicola suggested a very nice solution but I want to extend it.
r dataframe datatable
2
Trydf1$param_a/df2$param_b[match(df1$user_id,df2$user_id)]
.
– nicola
Nov 19 '18 at 14:02
@nicola it doens't work
– SteveS
Nov 19 '18 at 14:35
@nicola, if I have param_a, param_k, param_p and param b is a vector of multiple numbers it doesn't work.
– SteveS
Nov 19 '18 at 14:37
It works on your example. You should state why it doesn't work and provide a better example. FWIW, these kinds of "it doesn't work", without nothing else are so frustrating and let me question why I'm still here on this site trying to help others.
– nicola
Nov 19 '18 at 14:41
@nicola, I am sorry. You are completely right. I just thought it's a generic way to do this, but when I did apply it on more variables it didn't work. Can you please advise? I will update my question shortly.
– SteveS
Nov 19 '18 at 14:43
add a comment |
I have a data frame like this:
df1 <- structure(list(user_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), param_a = c(123,
2.3, -9, 1, -0.03333, 4, -41, -12, 0.89)), .Names = c("user_id",
"param_a"), row.names = c(NA, -9L), class = c("tbl_df", "tbl",
"data.frame"))
and another dataframe
of vectors:
df2 <- structure(list(user_id = c(1, 2, 3), param_b = c(34, 12, -0.89
)), .Names = c("user_id", "param_b"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
Now I want to divide each group in df1
by corresponding value in df2
:
For example for a group of user 1 divide each row by param_b
first vector:
user_id param_a
1 123/34
1 2.3/34
1 -9/34
2 1/12
2 -0.03333/12
2 4/12
....
for user 2 divide each row by param_b
second vector.
Please advise how can I divide a grouped by user dataframe
by a vector per each group?
P.S
If I have df1 extended to param_a, param_k, param_p
and df2 extended accordingly with param_b, param_l, param_r
How can I perform this kind of operation? @nicola suggested a very nice solution but I want to extend it.
r dataframe datatable
I have a data frame like this:
df1 <- structure(list(user_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3), param_a = c(123,
2.3, -9, 1, -0.03333, 4, -41, -12, 0.89)), .Names = c("user_id",
"param_a"), row.names = c(NA, -9L), class = c("tbl_df", "tbl",
"data.frame"))
and another dataframe
of vectors:
df2 <- structure(list(user_id = c(1, 2, 3), param_b = c(34, 12, -0.89
)), .Names = c("user_id", "param_b"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
Now I want to divide each group in df1
by corresponding value in df2
:
For example for a group of user 1 divide each row by param_b
first vector:
user_id param_a
1 123/34
1 2.3/34
1 -9/34
2 1/12
2 -0.03333/12
2 4/12
....
for user 2 divide each row by param_b
second vector.
Please advise how can I divide a grouped by user dataframe
by a vector per each group?
P.S
If I have df1 extended to param_a, param_k, param_p
and df2 extended accordingly with param_b, param_l, param_r
How can I perform this kind of operation? @nicola suggested a very nice solution but I want to extend it.
r dataframe datatable
r dataframe datatable
edited Nov 19 '18 at 14:45
SteveS
asked Nov 19 '18 at 13:59
SteveSSteveS
568211
568211
2
Trydf1$param_a/df2$param_b[match(df1$user_id,df2$user_id)]
.
– nicola
Nov 19 '18 at 14:02
@nicola it doens't work
– SteveS
Nov 19 '18 at 14:35
@nicola, if I have param_a, param_k, param_p and param b is a vector of multiple numbers it doesn't work.
– SteveS
Nov 19 '18 at 14:37
It works on your example. You should state why it doesn't work and provide a better example. FWIW, these kinds of "it doesn't work", without nothing else are so frustrating and let me question why I'm still here on this site trying to help others.
– nicola
Nov 19 '18 at 14:41
@nicola, I am sorry. You are completely right. I just thought it's a generic way to do this, but when I did apply it on more variables it didn't work. Can you please advise? I will update my question shortly.
– SteveS
Nov 19 '18 at 14:43
add a comment |
2
Trydf1$param_a/df2$param_b[match(df1$user_id,df2$user_id)]
.
– nicola
Nov 19 '18 at 14:02
@nicola it doens't work
– SteveS
Nov 19 '18 at 14:35
@nicola, if I have param_a, param_k, param_p and param b is a vector of multiple numbers it doesn't work.
– SteveS
Nov 19 '18 at 14:37
It works on your example. You should state why it doesn't work and provide a better example. FWIW, these kinds of "it doesn't work", without nothing else are so frustrating and let me question why I'm still here on this site trying to help others.
– nicola
Nov 19 '18 at 14:41
@nicola, I am sorry. You are completely right. I just thought it's a generic way to do this, but when I did apply it on more variables it didn't work. Can you please advise? I will update my question shortly.
– SteveS
Nov 19 '18 at 14:43
2
2
Try
df1$param_a/df2$param_b[match(df1$user_id,df2$user_id)]
.– nicola
Nov 19 '18 at 14:02
Try
df1$param_a/df2$param_b[match(df1$user_id,df2$user_id)]
.– nicola
Nov 19 '18 at 14:02
@nicola it doens't work
– SteveS
Nov 19 '18 at 14:35
@nicola it doens't work
– SteveS
Nov 19 '18 at 14:35
@nicola, if I have param_a, param_k, param_p and param b is a vector of multiple numbers it doesn't work.
– SteveS
Nov 19 '18 at 14:37
@nicola, if I have param_a, param_k, param_p and param b is a vector of multiple numbers it doesn't work.
– SteveS
Nov 19 '18 at 14:37
It works on your example. You should state why it doesn't work and provide a better example. FWIW, these kinds of "it doesn't work", without nothing else are so frustrating and let me question why I'm still here on this site trying to help others.
– nicola
Nov 19 '18 at 14:41
It works on your example. You should state why it doesn't work and provide a better example. FWIW, these kinds of "it doesn't work", without nothing else are so frustrating and let me question why I'm still here on this site trying to help others.
– nicola
Nov 19 '18 at 14:41
@nicola, I am sorry. You are completely right. I just thought it's a generic way to do this, but when I did apply it on more variables it didn't work. Can you please advise? I will update my question shortly.
– SteveS
Nov 19 '18 at 14:43
@nicola, I am sorry. You are completely right. I just thought it's a generic way to do this, but when I did apply it on more variables it didn't work. Can you please advise? I will update my question shortly.
– SteveS
Nov 19 '18 at 14:43
add a comment |
1 Answer
1
active
oldest
votes
Something like this?
df1%>%
left_join(df2)%>%
mutate(result=param_a/param_b)
Joining, by = "user_id"
# A tibble: 9 x 4
user_id param_a param_b result
<dbl> <dbl> <dbl> <dbl>
1 1 123 34 3.62
2 1 2.3 34 0.0676
3 1 -9 34 -0.265
4 2 1 12 0.0833
5 2 -0.0333 12 -0.00278
6 2 4 12 0.333
7 3 -41 -0.89 46.1
8 3 -12 -0.89 13.5
9 3 0.89 -0.89 -1
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%2f53376268%2fdataframe-divide-each-group-by-a-vector-corresponding-to-each-group-in-r%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
Something like this?
df1%>%
left_join(df2)%>%
mutate(result=param_a/param_b)
Joining, by = "user_id"
# A tibble: 9 x 4
user_id param_a param_b result
<dbl> <dbl> <dbl> <dbl>
1 1 123 34 3.62
2 1 2.3 34 0.0676
3 1 -9 34 -0.265
4 2 1 12 0.0833
5 2 -0.0333 12 -0.00278
6 2 4 12 0.333
7 3 -41 -0.89 46.1
8 3 -12 -0.89 13.5
9 3 0.89 -0.89 -1
add a comment |
Something like this?
df1%>%
left_join(df2)%>%
mutate(result=param_a/param_b)
Joining, by = "user_id"
# A tibble: 9 x 4
user_id param_a param_b result
<dbl> <dbl> <dbl> <dbl>
1 1 123 34 3.62
2 1 2.3 34 0.0676
3 1 -9 34 -0.265
4 2 1 12 0.0833
5 2 -0.0333 12 -0.00278
6 2 4 12 0.333
7 3 -41 -0.89 46.1
8 3 -12 -0.89 13.5
9 3 0.89 -0.89 -1
add a comment |
Something like this?
df1%>%
left_join(df2)%>%
mutate(result=param_a/param_b)
Joining, by = "user_id"
# A tibble: 9 x 4
user_id param_a param_b result
<dbl> <dbl> <dbl> <dbl>
1 1 123 34 3.62
2 1 2.3 34 0.0676
3 1 -9 34 -0.265
4 2 1 12 0.0833
5 2 -0.0333 12 -0.00278
6 2 4 12 0.333
7 3 -41 -0.89 46.1
8 3 -12 -0.89 13.5
9 3 0.89 -0.89 -1
Something like this?
df1%>%
left_join(df2)%>%
mutate(result=param_a/param_b)
Joining, by = "user_id"
# A tibble: 9 x 4
user_id param_a param_b result
<dbl> <dbl> <dbl> <dbl>
1 1 123 34 3.62
2 1 2.3 34 0.0676
3 1 -9 34 -0.265
4 2 1 12 0.0833
5 2 -0.0333 12 -0.00278
6 2 4 12 0.333
7 3 -41 -0.89 46.1
8 3 -12 -0.89 13.5
9 3 0.89 -0.89 -1
answered Nov 19 '18 at 14:03
jyjekjyjek
1,504312
1,504312
add a comment |
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.
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%2f53376268%2fdataframe-divide-each-group-by-a-vector-corresponding-to-each-group-in-r%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
2
Try
df1$param_a/df2$param_b[match(df1$user_id,df2$user_id)]
.– nicola
Nov 19 '18 at 14:02
@nicola it doens't work
– SteveS
Nov 19 '18 at 14:35
@nicola, if I have param_a, param_k, param_p and param b is a vector of multiple numbers it doesn't work.
– SteveS
Nov 19 '18 at 14:37
It works on your example. You should state why it doesn't work and provide a better example. FWIW, these kinds of "it doesn't work", without nothing else are so frustrating and let me question why I'm still here on this site trying to help others.
– nicola
Nov 19 '18 at 14:41
@nicola, I am sorry. You are completely right. I just thought it's a generic way to do this, but when I did apply it on more variables it didn't work. Can you please advise? I will update my question shortly.
– SteveS
Nov 19 '18 at 14:43