mean of some values over a column before reaching 0 and reset
up vote
1
down vote
favorite
I want to get the mean of some values over a column in the following way:
Computing the mean of those values where there is no 0 ,then when finding 0, reset and get the mean of the next values until find 0 again, and so on..
My data looks like:
len value
0 0.0000
0 0.0000
1 307.4463
2 308.0341
0 0.0000
0 0.0000
0 0.0000
1 305.7210
0 0.0000
Here is a sample:
dput(sdat)
structure(list(len = c(0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0), value = c(0,
0, 0, 0, 307.446310279931, 308.034096297716, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 305.720985439491, 0, 0, 0, 0, 0, 0, 308.599408144233,
308.511240241565, 305.843728990264, 0, 0, 0, 0, 0)), .Names = c("len",
"value"), row.names = 250:280, class = "data.frame")
So, I want to get another column with the means over "value". E.g. mean of (307.4463,308.0341), then 305.72..and so on..
My first try is using ave:
sdat$mv <- ave( sdat[,"value"], (sdat[,"value"])==0,
FUN=function(x) mean(x,na.rm=T))
So, the output I wish would be:
len value mv
0 0.0000 0
0 0.0000 0
1 307.4463 0
2 308.0341 307.7402 # mean of the previous 2-values
0 0.0000 0
0 0.0000 0
0 0.0000 0
1 305.7210 3015.7210
...................
But it's not working properly..I get wrong values..and if possible I'd to get in the resulting column only one value with the mean value (in the final position before the next 0) and the rest with 0 or NA should be fine .
Any idea/suggestion?
Many thanks,
r
add a comment |
up vote
1
down vote
favorite
I want to get the mean of some values over a column in the following way:
Computing the mean of those values where there is no 0 ,then when finding 0, reset and get the mean of the next values until find 0 again, and so on..
My data looks like:
len value
0 0.0000
0 0.0000
1 307.4463
2 308.0341
0 0.0000
0 0.0000
0 0.0000
1 305.7210
0 0.0000
Here is a sample:
dput(sdat)
structure(list(len = c(0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0), value = c(0,
0, 0, 0, 307.446310279931, 308.034096297716, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 305.720985439491, 0, 0, 0, 0, 0, 0, 308.599408144233,
308.511240241565, 305.843728990264, 0, 0, 0, 0, 0)), .Names = c("len",
"value"), row.names = 250:280, class = "data.frame")
So, I want to get another column with the means over "value". E.g. mean of (307.4463,308.0341), then 305.72..and so on..
My first try is using ave:
sdat$mv <- ave( sdat[,"value"], (sdat[,"value"])==0,
FUN=function(x) mean(x,na.rm=T))
So, the output I wish would be:
len value mv
0 0.0000 0
0 0.0000 0
1 307.4463 0
2 308.0341 307.7402 # mean of the previous 2-values
0 0.0000 0
0 0.0000 0
0 0.0000 0
1 305.7210 3015.7210
...................
But it's not working properly..I get wrong values..and if possible I'd to get in the resulting column only one value with the mean value (in the final position before the next 0) and the rest with 0 or NA should be fine .
Any idea/suggestion?
Many thanks,
r
Yes, I just updated the post, thanks!
– user3231352
Nov 12 at 10:21
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to get the mean of some values over a column in the following way:
Computing the mean of those values where there is no 0 ,then when finding 0, reset and get the mean of the next values until find 0 again, and so on..
My data looks like:
len value
0 0.0000
0 0.0000
1 307.4463
2 308.0341
0 0.0000
0 0.0000
0 0.0000
1 305.7210
0 0.0000
Here is a sample:
dput(sdat)
structure(list(len = c(0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0), value = c(0,
0, 0, 0, 307.446310279931, 308.034096297716, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 305.720985439491, 0, 0, 0, 0, 0, 0, 308.599408144233,
308.511240241565, 305.843728990264, 0, 0, 0, 0, 0)), .Names = c("len",
"value"), row.names = 250:280, class = "data.frame")
So, I want to get another column with the means over "value". E.g. mean of (307.4463,308.0341), then 305.72..and so on..
My first try is using ave:
sdat$mv <- ave( sdat[,"value"], (sdat[,"value"])==0,
FUN=function(x) mean(x,na.rm=T))
So, the output I wish would be:
len value mv
0 0.0000 0
0 0.0000 0
1 307.4463 0
2 308.0341 307.7402 # mean of the previous 2-values
0 0.0000 0
0 0.0000 0
0 0.0000 0
1 305.7210 3015.7210
...................
But it's not working properly..I get wrong values..and if possible I'd to get in the resulting column only one value with the mean value (in the final position before the next 0) and the rest with 0 or NA should be fine .
Any idea/suggestion?
Many thanks,
r
I want to get the mean of some values over a column in the following way:
Computing the mean of those values where there is no 0 ,then when finding 0, reset and get the mean of the next values until find 0 again, and so on..
My data looks like:
len value
0 0.0000
0 0.0000
1 307.4463
2 308.0341
0 0.0000
0 0.0000
0 0.0000
1 305.7210
0 0.0000
Here is a sample:
dput(sdat)
structure(list(len = c(0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0), value = c(0,
0, 0, 0, 307.446310279931, 308.034096297716, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 305.720985439491, 0, 0, 0, 0, 0, 0, 308.599408144233,
308.511240241565, 305.843728990264, 0, 0, 0, 0, 0)), .Names = c("len",
"value"), row.names = 250:280, class = "data.frame")
So, I want to get another column with the means over "value". E.g. mean of (307.4463,308.0341), then 305.72..and so on..
My first try is using ave:
sdat$mv <- ave( sdat[,"value"], (sdat[,"value"])==0,
FUN=function(x) mean(x,na.rm=T))
So, the output I wish would be:
len value mv
0 0.0000 0
0 0.0000 0
1 307.4463 0
2 308.0341 307.7402 # mean of the previous 2-values
0 0.0000 0
0 0.0000 0
0 0.0000 0
1 305.7210 3015.7210
...................
But it's not working properly..I get wrong values..and if possible I'd to get in the resulting column only one value with the mean value (in the final position before the next 0) and the rest with 0 or NA should be fine .
Any idea/suggestion?
Many thanks,
r
r
edited Nov 12 at 10:21
asked Nov 12 at 10:11
user3231352
400316
400316
Yes, I just updated the post, thanks!
– user3231352
Nov 12 at 10:21
add a comment |
Yes, I just updated the post, thanks!
– user3231352
Nov 12 at 10:21
Yes, I just updated the post, thanks!
– user3231352
Nov 12 at 10:21
Yes, I just updated the post, thanks!
– user3231352
Nov 12 at 10:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You are getting wrong results because the grouping variable doesn't have diferent values for each segment of zero-not zero-etc.
A cumsum
trick will produce a good grouping variable.
f <- c(0L, cumsum(abs(diff(sdat$value != 0))))
ave(sdat$value, f, FUN = mean, na.rm = TRUE)
res <- cbind(sdat, Mean = ave(sdat$value, f, FUN = mean, na.rm = TRUE))
head(res, 10)
# len value Mean
#250 0 0.0000 0.0000
#251 0 0.0000 0.0000
#252 0 0.0000 0.0000
#253 0 0.0000 0.0000
#254 1 307.4463 307.7402
#255 2 308.0341 307.7402
#256 0 0.0000 0.0000
#257 0 0.0000 0.0000
#258 0 0.0000 0.0000
#259 0 0.0000 0.0000
Final cleanup.
rm(f)
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
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',
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%2f53259934%2fmean-of-some-values-over-a-column-before-reaching-0-and-reset%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
up vote
0
down vote
accepted
You are getting wrong results because the grouping variable doesn't have diferent values for each segment of zero-not zero-etc.
A cumsum
trick will produce a good grouping variable.
f <- c(0L, cumsum(abs(diff(sdat$value != 0))))
ave(sdat$value, f, FUN = mean, na.rm = TRUE)
res <- cbind(sdat, Mean = ave(sdat$value, f, FUN = mean, na.rm = TRUE))
head(res, 10)
# len value Mean
#250 0 0.0000 0.0000
#251 0 0.0000 0.0000
#252 0 0.0000 0.0000
#253 0 0.0000 0.0000
#254 1 307.4463 307.7402
#255 2 308.0341 307.7402
#256 0 0.0000 0.0000
#257 0 0.0000 0.0000
#258 0 0.0000 0.0000
#259 0 0.0000 0.0000
Final cleanup.
rm(f)
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
add a comment |
up vote
0
down vote
accepted
You are getting wrong results because the grouping variable doesn't have diferent values for each segment of zero-not zero-etc.
A cumsum
trick will produce a good grouping variable.
f <- c(0L, cumsum(abs(diff(sdat$value != 0))))
ave(sdat$value, f, FUN = mean, na.rm = TRUE)
res <- cbind(sdat, Mean = ave(sdat$value, f, FUN = mean, na.rm = TRUE))
head(res, 10)
# len value Mean
#250 0 0.0000 0.0000
#251 0 0.0000 0.0000
#252 0 0.0000 0.0000
#253 0 0.0000 0.0000
#254 1 307.4463 307.7402
#255 2 308.0341 307.7402
#256 0 0.0000 0.0000
#257 0 0.0000 0.0000
#258 0 0.0000 0.0000
#259 0 0.0000 0.0000
Final cleanup.
rm(f)
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are getting wrong results because the grouping variable doesn't have diferent values for each segment of zero-not zero-etc.
A cumsum
trick will produce a good grouping variable.
f <- c(0L, cumsum(abs(diff(sdat$value != 0))))
ave(sdat$value, f, FUN = mean, na.rm = TRUE)
res <- cbind(sdat, Mean = ave(sdat$value, f, FUN = mean, na.rm = TRUE))
head(res, 10)
# len value Mean
#250 0 0.0000 0.0000
#251 0 0.0000 0.0000
#252 0 0.0000 0.0000
#253 0 0.0000 0.0000
#254 1 307.4463 307.7402
#255 2 308.0341 307.7402
#256 0 0.0000 0.0000
#257 0 0.0000 0.0000
#258 0 0.0000 0.0000
#259 0 0.0000 0.0000
Final cleanup.
rm(f)
You are getting wrong results because the grouping variable doesn't have diferent values for each segment of zero-not zero-etc.
A cumsum
trick will produce a good grouping variable.
f <- c(0L, cumsum(abs(diff(sdat$value != 0))))
ave(sdat$value, f, FUN = mean, na.rm = TRUE)
res <- cbind(sdat, Mean = ave(sdat$value, f, FUN = mean, na.rm = TRUE))
head(res, 10)
# len value Mean
#250 0 0.0000 0.0000
#251 0 0.0000 0.0000
#252 0 0.0000 0.0000
#253 0 0.0000 0.0000
#254 1 307.4463 307.7402
#255 2 308.0341 307.7402
#256 0 0.0000 0.0000
#257 0 0.0000 0.0000
#258 0 0.0000 0.0000
#259 0 0.0000 0.0000
Final cleanup.
rm(f)
answered Nov 12 at 10:22
Rui Barradas
15.4k41730
15.4k41730
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
add a comment |
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
You're right, thanks for the suggestion, this approach works fine! Thanks for the help.
– user3231352
Nov 12 at 10:36
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%2f53259934%2fmean-of-some-values-over-a-column-before-reaching-0-and-reset%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
Yes, I just updated the post, thanks!
– user3231352
Nov 12 at 10:21