Creating a *NEW* multi-conditional (function) column in R
I am trying to create a new conditional column based on two conditionals. I want to find the mean of columns A, B, and C based on the name and weeks leading UP TO (but not including) the week in the corresponding row. Let us take Joe as an example. For row 1, we would have no data in the new columns. For row 2 would have the "mean" of week 1 data. for Row 3, we want the mean of week 1 and week 2 data. In practice, we can have more weeks than 3, but I wanted to simplify the example. I would like to avoid loops if possible.
This is fairly easy to do in Excel with the Averageifs() function but I want to automate/consolidate the process with R
my data looks something like this:
Name Week A B C
Joe 1 5 6 7
Joe 2 4 5 6
Joe 3 2 3 4
Tim 1 7 8 9
Tim 2 5 4 6
Tim 4 3 5 4
Bob 1 9 8 7
Bob 3 8 5 2
Bob 4 4 5 3
The new data would look something like this:
Name Week A B C A_2 B_2 C_2
Joe 1 5 6 7 NA NA NA
Joe 2 4 5 6 5 6 7
Joe 3 2 3 4 4.5 5.5 6.5
Tim 1 7 8 9 NA NA NA
Tim 2 5 4 6 7 8 9
Tim 4 3 5 4 6 6 7.5
Bob 1 9 8 7 NA NA NA
Bob 3 8 5 2 9 8 7
Bob 4 4 5 3 8.5 6.5 4.5
Thank you for any help you can provide! I am semi-new to R and have been struggling with this problem
r conditional mean calculated-columns moving-average
add a comment |
I am trying to create a new conditional column based on two conditionals. I want to find the mean of columns A, B, and C based on the name and weeks leading UP TO (but not including) the week in the corresponding row. Let us take Joe as an example. For row 1, we would have no data in the new columns. For row 2 would have the "mean" of week 1 data. for Row 3, we want the mean of week 1 and week 2 data. In practice, we can have more weeks than 3, but I wanted to simplify the example. I would like to avoid loops if possible.
This is fairly easy to do in Excel with the Averageifs() function but I want to automate/consolidate the process with R
my data looks something like this:
Name Week A B C
Joe 1 5 6 7
Joe 2 4 5 6
Joe 3 2 3 4
Tim 1 7 8 9
Tim 2 5 4 6
Tim 4 3 5 4
Bob 1 9 8 7
Bob 3 8 5 2
Bob 4 4 5 3
The new data would look something like this:
Name Week A B C A_2 B_2 C_2
Joe 1 5 6 7 NA NA NA
Joe 2 4 5 6 5 6 7
Joe 3 2 3 4 4.5 5.5 6.5
Tim 1 7 8 9 NA NA NA
Tim 2 5 4 6 7 8 9
Tim 4 3 5 4 6 6 7.5
Bob 1 9 8 7 NA NA NA
Bob 3 8 5 2 9 8 7
Bob 4 4 5 3 8.5 6.5 4.5
Thank you for any help you can provide! I am semi-new to R and have been struggling with this problem
r conditional mean calculated-columns moving-average
How to calculate the Cumulative Average for some numbers?, e.g.cumsum(x) / seq_along(x)
– Henrik
Nov 14 '18 at 7:38
add a comment |
I am trying to create a new conditional column based on two conditionals. I want to find the mean of columns A, B, and C based on the name and weeks leading UP TO (but not including) the week in the corresponding row. Let us take Joe as an example. For row 1, we would have no data in the new columns. For row 2 would have the "mean" of week 1 data. for Row 3, we want the mean of week 1 and week 2 data. In practice, we can have more weeks than 3, but I wanted to simplify the example. I would like to avoid loops if possible.
This is fairly easy to do in Excel with the Averageifs() function but I want to automate/consolidate the process with R
my data looks something like this:
Name Week A B C
Joe 1 5 6 7
Joe 2 4 5 6
Joe 3 2 3 4
Tim 1 7 8 9
Tim 2 5 4 6
Tim 4 3 5 4
Bob 1 9 8 7
Bob 3 8 5 2
Bob 4 4 5 3
The new data would look something like this:
Name Week A B C A_2 B_2 C_2
Joe 1 5 6 7 NA NA NA
Joe 2 4 5 6 5 6 7
Joe 3 2 3 4 4.5 5.5 6.5
Tim 1 7 8 9 NA NA NA
Tim 2 5 4 6 7 8 9
Tim 4 3 5 4 6 6 7.5
Bob 1 9 8 7 NA NA NA
Bob 3 8 5 2 9 8 7
Bob 4 4 5 3 8.5 6.5 4.5
Thank you for any help you can provide! I am semi-new to R and have been struggling with this problem
r conditional mean calculated-columns moving-average
I am trying to create a new conditional column based on two conditionals. I want to find the mean of columns A, B, and C based on the name and weeks leading UP TO (but not including) the week in the corresponding row. Let us take Joe as an example. For row 1, we would have no data in the new columns. For row 2 would have the "mean" of week 1 data. for Row 3, we want the mean of week 1 and week 2 data. In practice, we can have more weeks than 3, but I wanted to simplify the example. I would like to avoid loops if possible.
This is fairly easy to do in Excel with the Averageifs() function but I want to automate/consolidate the process with R
my data looks something like this:
Name Week A B C
Joe 1 5 6 7
Joe 2 4 5 6
Joe 3 2 3 4
Tim 1 7 8 9
Tim 2 5 4 6
Tim 4 3 5 4
Bob 1 9 8 7
Bob 3 8 5 2
Bob 4 4 5 3
The new data would look something like this:
Name Week A B C A_2 B_2 C_2
Joe 1 5 6 7 NA NA NA
Joe 2 4 5 6 5 6 7
Joe 3 2 3 4 4.5 5.5 6.5
Tim 1 7 8 9 NA NA NA
Tim 2 5 4 6 7 8 9
Tim 4 3 5 4 6 6 7.5
Bob 1 9 8 7 NA NA NA
Bob 3 8 5 2 9 8 7
Bob 4 4 5 3 8.5 6.5 4.5
Thank you for any help you can provide! I am semi-new to R and have been struggling with this problem
r conditional mean calculated-columns moving-average
r conditional mean calculated-columns moving-average
asked Nov 13 '18 at 14:24
KoalaKoala
132
132
How to calculate the Cumulative Average for some numbers?, e.g.cumsum(x) / seq_along(x)
– Henrik
Nov 14 '18 at 7:38
add a comment |
How to calculate the Cumulative Average for some numbers?, e.g.cumsum(x) / seq_along(x)
– Henrik
Nov 14 '18 at 7:38
How to calculate the Cumulative Average for some numbers?, e.g.
cumsum(x) / seq_along(x)
– Henrik
Nov 14 '18 at 7:38
How to calculate the Cumulative Average for some numbers?, e.g.
cumsum(x) / seq_along(x)
– Henrik
Nov 14 '18 at 7:38
add a comment |
2 Answers
2
active
oldest
votes
Here's a way with dplyr
package -
df %>%
group_by(Name) %>%
arrange(Name, Week) %>%
mutate(
A_2 = lag(cummean(A)),
B_2 = lag(cummean(B)),
C_2 = lag(cummean(C))
) %>%
ungroup()
# A tibble: 9 x 8
Name Week A B C A_2 B_2 C_2
<fct> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 Bob 1 9 8 7 NA NA NA
2 Bob 3 8 5 2 9.00 8.00 7.00
3 Bob 4 4 5 3 8.50 6.50 4.50
4 Joe 1 5 6 7 NA NA NA
5 Joe 2 4 5 6 5.00 6.00 7.00
6 Joe 3 2 3 4 4.50 5.50 6.50
7 Tim 1 7 8 9 NA NA NA
8 Tim 2 5 4 6 7.00 8.00 9.00
9 Tim 4 3 5 4 6.00 6.00 7.50
Data -
df <- structure(list(Name = structure(c(2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L), .Label = c("Bob", "Joe", "Tim"), class = "factor"),
Week = c(1L, 2L, 3L, 1L, 2L, 4L, 1L, 3L, 4L), A = c(5L, 4L,
2L, 7L, 5L, 3L, 9L, 8L, 4L), B = c(6L, 5L, 3L, 8L, 4L, 5L,
8L, 5L, 5L), C = c(7L, 6L, 4L, 9L, 6L, 4L, 7L, 2L, 3L)), .Names = c("Name",
"Week", "A", "B", "C"), class = "data.frame", row.names = c(NA,
-9L))
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
use this function -cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
add a comment |
A data.table
approach:
library(data.table)
setDT(df)[order(Name, Week),][, `:=` (
A_mean = shift(cummean(A)),
B_mean = shift(cummean(B)),
C_mean = shift(cummean(C))
), by = Name]
Note that at the end is just for printing the result.
Output:
Name Week A B C A_mean B_mean C_mean
1: Bob 1 9 8 7 NA NA NA
2: Bob 3 8 5 2 9.0 8.0 7.0
3: Bob 4 4 5 3 8.5 6.5 4.5
4: Joe 1 5 6 7 NA NA NA
5: Joe 2 4 5 6 5.0 6.0 7.0
6: Joe 3 2 3 4 4.5 5.5 6.5
7: Tim 1 7 8 9 NA NA NA
8: Tim 2 5 4 6 7.0 8.0 9.0
9: Tim 4 3 5 4 6.0 6.0 7.5
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%2f53283176%2fcreating-a-new-multi-conditional-function-column-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a way with dplyr
package -
df %>%
group_by(Name) %>%
arrange(Name, Week) %>%
mutate(
A_2 = lag(cummean(A)),
B_2 = lag(cummean(B)),
C_2 = lag(cummean(C))
) %>%
ungroup()
# A tibble: 9 x 8
Name Week A B C A_2 B_2 C_2
<fct> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 Bob 1 9 8 7 NA NA NA
2 Bob 3 8 5 2 9.00 8.00 7.00
3 Bob 4 4 5 3 8.50 6.50 4.50
4 Joe 1 5 6 7 NA NA NA
5 Joe 2 4 5 6 5.00 6.00 7.00
6 Joe 3 2 3 4 4.50 5.50 6.50
7 Tim 1 7 8 9 NA NA NA
8 Tim 2 5 4 6 7.00 8.00 9.00
9 Tim 4 3 5 4 6.00 6.00 7.50
Data -
df <- structure(list(Name = structure(c(2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L), .Label = c("Bob", "Joe", "Tim"), class = "factor"),
Week = c(1L, 2L, 3L, 1L, 2L, 4L, 1L, 3L, 4L), A = c(5L, 4L,
2L, 7L, 5L, 3L, 9L, 8L, 4L), B = c(6L, 5L, 3L, 8L, 4L, 5L,
8L, 5L, 5L), C = c(7L, 6L, 4L, 9L, 6L, 4L, 7L, 2L, 3L)), .Names = c("Name",
"Week", "A", "B", "C"), class = "data.frame", row.names = c(NA,
-9L))
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
use this function -cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
add a comment |
Here's a way with dplyr
package -
df %>%
group_by(Name) %>%
arrange(Name, Week) %>%
mutate(
A_2 = lag(cummean(A)),
B_2 = lag(cummean(B)),
C_2 = lag(cummean(C))
) %>%
ungroup()
# A tibble: 9 x 8
Name Week A B C A_2 B_2 C_2
<fct> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 Bob 1 9 8 7 NA NA NA
2 Bob 3 8 5 2 9.00 8.00 7.00
3 Bob 4 4 5 3 8.50 6.50 4.50
4 Joe 1 5 6 7 NA NA NA
5 Joe 2 4 5 6 5.00 6.00 7.00
6 Joe 3 2 3 4 4.50 5.50 6.50
7 Tim 1 7 8 9 NA NA NA
8 Tim 2 5 4 6 7.00 8.00 9.00
9 Tim 4 3 5 4 6.00 6.00 7.50
Data -
df <- structure(list(Name = structure(c(2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L), .Label = c("Bob", "Joe", "Tim"), class = "factor"),
Week = c(1L, 2L, 3L, 1L, 2L, 4L, 1L, 3L, 4L), A = c(5L, 4L,
2L, 7L, 5L, 3L, 9L, 8L, 4L), B = c(6L, 5L, 3L, 8L, 4L, 5L,
8L, 5L, 5L), C = c(7L, 6L, 4L, 9L, 6L, 4L, 7L, 2L, 3L)), .Names = c("Name",
"Week", "A", "B", "C"), class = "data.frame", row.names = c(NA,
-9L))
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
use this function -cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
add a comment |
Here's a way with dplyr
package -
df %>%
group_by(Name) %>%
arrange(Name, Week) %>%
mutate(
A_2 = lag(cummean(A)),
B_2 = lag(cummean(B)),
C_2 = lag(cummean(C))
) %>%
ungroup()
# A tibble: 9 x 8
Name Week A B C A_2 B_2 C_2
<fct> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 Bob 1 9 8 7 NA NA NA
2 Bob 3 8 5 2 9.00 8.00 7.00
3 Bob 4 4 5 3 8.50 6.50 4.50
4 Joe 1 5 6 7 NA NA NA
5 Joe 2 4 5 6 5.00 6.00 7.00
6 Joe 3 2 3 4 4.50 5.50 6.50
7 Tim 1 7 8 9 NA NA NA
8 Tim 2 5 4 6 7.00 8.00 9.00
9 Tim 4 3 5 4 6.00 6.00 7.50
Data -
df <- structure(list(Name = structure(c(2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L), .Label = c("Bob", "Joe", "Tim"), class = "factor"),
Week = c(1L, 2L, 3L, 1L, 2L, 4L, 1L, 3L, 4L), A = c(5L, 4L,
2L, 7L, 5L, 3L, 9L, 8L, 4L), B = c(6L, 5L, 3L, 8L, 4L, 5L,
8L, 5L, 5L), C = c(7L, 6L, 4L, 9L, 6L, 4L, 7L, 2L, 3L)), .Names = c("Name",
"Week", "A", "B", "C"), class = "data.frame", row.names = c(NA,
-9L))
Here's a way with dplyr
package -
df %>%
group_by(Name) %>%
arrange(Name, Week) %>%
mutate(
A_2 = lag(cummean(A)),
B_2 = lag(cummean(B)),
C_2 = lag(cummean(C))
) %>%
ungroup()
# A tibble: 9 x 8
Name Week A B C A_2 B_2 C_2
<fct> <int> <int> <int> <int> <dbl> <dbl> <dbl>
1 Bob 1 9 8 7 NA NA NA
2 Bob 3 8 5 2 9.00 8.00 7.00
3 Bob 4 4 5 3 8.50 6.50 4.50
4 Joe 1 5 6 7 NA NA NA
5 Joe 2 4 5 6 5.00 6.00 7.00
6 Joe 3 2 3 4 4.50 5.50 6.50
7 Tim 1 7 8 9 NA NA NA
8 Tim 2 5 4 6 7.00 8.00 9.00
9 Tim 4 3 5 4 6.00 6.00 7.50
Data -
df <- structure(list(Name = structure(c(2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L), .Label = c("Bob", "Joe", "Tim"), class = "factor"),
Week = c(1L, 2L, 3L, 1L, 2L, 4L, 1L, 3L, 4L), A = c(5L, 4L,
2L, 7L, 5L, 3L, 9L, 8L, 4L), B = c(6L, 5L, 3L, 8L, 4L, 5L,
8L, 5L, 5L), C = c(7L, 6L, 4L, 9L, 6L, 4L, 7L, 2L, 3L)), .Names = c("Name",
"Week", "A", "B", "C"), class = "data.frame", row.names = c(NA,
-9L))
answered Nov 13 '18 at 15:05
ShreeShree
3,3461323
3,3461323
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
use this function -cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
add a comment |
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
use this function -cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Perfect! Thank you!!!
– Koala
Nov 13 '18 at 16:12
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
Would you happen to know a way doing this with Standard deviation as well? Essentially, a "cumulative standard deviation"
– Koala
Nov 13 '18 at 16:20
use this function -
cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
use this function -
cumsd <- function(x) { sapply(1:length(x), function(a) sd(x[1:a])) }
– Shree
Nov 13 '18 at 16:28
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
A+++ you're are the best!
– Koala
Nov 13 '18 at 17:49
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
One more question, what if I wanted the last 3 (or n) weeks? and not all weeks leading up to the current week?
– Koala
Nov 17 '18 at 4:48
add a comment |
A data.table
approach:
library(data.table)
setDT(df)[order(Name, Week),][, `:=` (
A_mean = shift(cummean(A)),
B_mean = shift(cummean(B)),
C_mean = shift(cummean(C))
), by = Name]
Note that at the end is just for printing the result.
Output:
Name Week A B C A_mean B_mean C_mean
1: Bob 1 9 8 7 NA NA NA
2: Bob 3 8 5 2 9.0 8.0 7.0
3: Bob 4 4 5 3 8.5 6.5 4.5
4: Joe 1 5 6 7 NA NA NA
5: Joe 2 4 5 6 5.0 6.0 7.0
6: Joe 3 2 3 4 4.5 5.5 6.5
7: Tim 1 7 8 9 NA NA NA
8: Tim 2 5 4 6 7.0 8.0 9.0
9: Tim 4 3 5 4 6.0 6.0 7.5
add a comment |
A data.table
approach:
library(data.table)
setDT(df)[order(Name, Week),][, `:=` (
A_mean = shift(cummean(A)),
B_mean = shift(cummean(B)),
C_mean = shift(cummean(C))
), by = Name]
Note that at the end is just for printing the result.
Output:
Name Week A B C A_mean B_mean C_mean
1: Bob 1 9 8 7 NA NA NA
2: Bob 3 8 5 2 9.0 8.0 7.0
3: Bob 4 4 5 3 8.5 6.5 4.5
4: Joe 1 5 6 7 NA NA NA
5: Joe 2 4 5 6 5.0 6.0 7.0
6: Joe 3 2 3 4 4.5 5.5 6.5
7: Tim 1 7 8 9 NA NA NA
8: Tim 2 5 4 6 7.0 8.0 9.0
9: Tim 4 3 5 4 6.0 6.0 7.5
add a comment |
A data.table
approach:
library(data.table)
setDT(df)[order(Name, Week),][, `:=` (
A_mean = shift(cummean(A)),
B_mean = shift(cummean(B)),
C_mean = shift(cummean(C))
), by = Name]
Note that at the end is just for printing the result.
Output:
Name Week A B C A_mean B_mean C_mean
1: Bob 1 9 8 7 NA NA NA
2: Bob 3 8 5 2 9.0 8.0 7.0
3: Bob 4 4 5 3 8.5 6.5 4.5
4: Joe 1 5 6 7 NA NA NA
5: Joe 2 4 5 6 5.0 6.0 7.0
6: Joe 3 2 3 4 4.5 5.5 6.5
7: Tim 1 7 8 9 NA NA NA
8: Tim 2 5 4 6 7.0 8.0 9.0
9: Tim 4 3 5 4 6.0 6.0 7.5
A data.table
approach:
library(data.table)
setDT(df)[order(Name, Week),][, `:=` (
A_mean = shift(cummean(A)),
B_mean = shift(cummean(B)),
C_mean = shift(cummean(C))
), by = Name]
Note that at the end is just for printing the result.
Output:
Name Week A B C A_mean B_mean C_mean
1: Bob 1 9 8 7 NA NA NA
2: Bob 3 8 5 2 9.0 8.0 7.0
3: Bob 4 4 5 3 8.5 6.5 4.5
4: Joe 1 5 6 7 NA NA NA
5: Joe 2 4 5 6 5.0 6.0 7.0
6: Joe 3 2 3 4 4.5 5.5 6.5
7: Tim 1 7 8 9 NA NA NA
8: Tim 2 5 4 6 7.0 8.0 9.0
9: Tim 4 3 5 4 6.0 6.0 7.5
edited Nov 13 '18 at 15:40
answered Nov 13 '18 at 15:24
arg0nautarg0naut
2,142314
2,142314
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%2f53283176%2fcreating-a-new-multi-conditional-function-column-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
How to calculate the Cumulative Average for some numbers?, e.g.
cumsum(x) / seq_along(x)
– Henrik
Nov 14 '18 at 7:38