Mean of the values that have less than 10 months in stock











up vote
1
down vote

favorite












So I have a datatable (dt) with the following elements, an id of an Item ,the date and stock on that day.



ID        date             Stock
193sd 2016-03-23 12
238ds 2016-04-20 1
193sd 2017-09-12 1
...


So For example, an ID like 193sd has different dates



ID        date             Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2017-01-12 2
193sd 2016-02-11 1


I need to find from the newest date 10 months ago the mean of the stock, manually I know i should do the following operations:



For that ID 193sd




  1. SORT the dt$date from newest to oldest


  2. get M the number of months between date(i) and date(i+1)



    dates=c(dateNew, dateOld)
    dates2 <- strptime(dates, format = "%Y-%m-%d")
    dif <- diff(as.numeric(dates2)) # difference in seconds
    months = dif/(60 * 60 * 24 * 30) # months



  3. check if M<=10



  4. if true find mean of the stock for those dates for that ID and finish



    The mean is the sum of the values of dt$stock divided to 10



  5. if false repeat



As a programmer of other languages and new to R I always think for loops but im sure there is a better cleaner why when working with datatables.










share|improve this question
























  • can u be more specific...u r clubbing more questions in to one question or making different things ?
    – sai saran
    Nov 19 at 11:57










  • The question is so clear Finding the mean of stock 10 months , but I am trying to add as much details and approaches I did
    – Programmer Man
    Nov 19 at 11:58












  • mean of stock per month or per id basis or with the combination ?
    – sai saran
    Nov 19 at 12:00










  • For each ID obviously
    – Programmer Man
    Nov 19 at 12:01















up vote
1
down vote

favorite












So I have a datatable (dt) with the following elements, an id of an Item ,the date and stock on that day.



ID        date             Stock
193sd 2016-03-23 12
238ds 2016-04-20 1
193sd 2017-09-12 1
...


So For example, an ID like 193sd has different dates



ID        date             Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2017-01-12 2
193sd 2016-02-11 1


I need to find from the newest date 10 months ago the mean of the stock, manually I know i should do the following operations:



For that ID 193sd




  1. SORT the dt$date from newest to oldest


  2. get M the number of months between date(i) and date(i+1)



    dates=c(dateNew, dateOld)
    dates2 <- strptime(dates, format = "%Y-%m-%d")
    dif <- diff(as.numeric(dates2)) # difference in seconds
    months = dif/(60 * 60 * 24 * 30) # months



  3. check if M<=10



  4. if true find mean of the stock for those dates for that ID and finish



    The mean is the sum of the values of dt$stock divided to 10



  5. if false repeat



As a programmer of other languages and new to R I always think for loops but im sure there is a better cleaner why when working with datatables.










share|improve this question
























  • can u be more specific...u r clubbing more questions in to one question or making different things ?
    – sai saran
    Nov 19 at 11:57










  • The question is so clear Finding the mean of stock 10 months , but I am trying to add as much details and approaches I did
    – Programmer Man
    Nov 19 at 11:58












  • mean of stock per month or per id basis or with the combination ?
    – sai saran
    Nov 19 at 12:00










  • For each ID obviously
    – Programmer Man
    Nov 19 at 12:01













up vote
1
down vote

favorite









up vote
1
down vote

favorite











So I have a datatable (dt) with the following elements, an id of an Item ,the date and stock on that day.



ID        date             Stock
193sd 2016-03-23 12
238ds 2016-04-20 1
193sd 2017-09-12 1
...


So For example, an ID like 193sd has different dates



ID        date             Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2017-01-12 2
193sd 2016-02-11 1


I need to find from the newest date 10 months ago the mean of the stock, manually I know i should do the following operations:



For that ID 193sd




  1. SORT the dt$date from newest to oldest


  2. get M the number of months between date(i) and date(i+1)



    dates=c(dateNew, dateOld)
    dates2 <- strptime(dates, format = "%Y-%m-%d")
    dif <- diff(as.numeric(dates2)) # difference in seconds
    months = dif/(60 * 60 * 24 * 30) # months



  3. check if M<=10



  4. if true find mean of the stock for those dates for that ID and finish



    The mean is the sum of the values of dt$stock divided to 10



  5. if false repeat



As a programmer of other languages and new to R I always think for loops but im sure there is a better cleaner why when working with datatables.










share|improve this question















So I have a datatable (dt) with the following elements, an id of an Item ,the date and stock on that day.



ID        date             Stock
193sd 2016-03-23 12
238ds 2016-04-20 1
193sd 2017-09-12 1
...


So For example, an ID like 193sd has different dates



ID        date             Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2017-01-12 2
193sd 2016-02-11 1


I need to find from the newest date 10 months ago the mean of the stock, manually I know i should do the following operations:



For that ID 193sd




  1. SORT the dt$date from newest to oldest


  2. get M the number of months between date(i) and date(i+1)



    dates=c(dateNew, dateOld)
    dates2 <- strptime(dates, format = "%Y-%m-%d")
    dif <- diff(as.numeric(dates2)) # difference in seconds
    months = dif/(60 * 60 * 24 * 30) # months



  3. check if M<=10



  4. if true find mean of the stock for those dates for that ID and finish



    The mean is the sum of the values of dt$stock divided to 10



  5. if false repeat



As a programmer of other languages and new to R I always think for loops but im sure there is a better cleaner why when working with datatables.







r data.table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 14:14









Scarabee

3,59541843




3,59541843










asked Nov 19 at 11:46









Programmer Man

409115




409115












  • can u be more specific...u r clubbing more questions in to one question or making different things ?
    – sai saran
    Nov 19 at 11:57










  • The question is so clear Finding the mean of stock 10 months , but I am trying to add as much details and approaches I did
    – Programmer Man
    Nov 19 at 11:58












  • mean of stock per month or per id basis or with the combination ?
    – sai saran
    Nov 19 at 12:00










  • For each ID obviously
    – Programmer Man
    Nov 19 at 12:01


















  • can u be more specific...u r clubbing more questions in to one question or making different things ?
    – sai saran
    Nov 19 at 11:57










  • The question is so clear Finding the mean of stock 10 months , but I am trying to add as much details and approaches I did
    – Programmer Man
    Nov 19 at 11:58












  • mean of stock per month or per id basis or with the combination ?
    – sai saran
    Nov 19 at 12:00










  • For each ID obviously
    – Programmer Man
    Nov 19 at 12:01
















can u be more specific...u r clubbing more questions in to one question or making different things ?
– sai saran
Nov 19 at 11:57




can u be more specific...u r clubbing more questions in to one question or making different things ?
– sai saran
Nov 19 at 11:57












The question is so clear Finding the mean of stock 10 months , but I am trying to add as much details and approaches I did
– Programmer Man
Nov 19 at 11:58






The question is so clear Finding the mean of stock 10 months , but I am trying to add as much details and approaches I did
– Programmer Man
Nov 19 at 11:58














mean of stock per month or per id basis or with the combination ?
– sai saran
Nov 19 at 12:00




mean of stock per month or per id basis or with the combination ?
– sai saran
Nov 19 at 12:00












For each ID obviously
– Programmer Man
Nov 19 at 12:01




For each ID obviously
– Programmer Man
Nov 19 at 12:01












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










With the packages data.table and lubridate you could do the followin'



library(data.table); library(lubridate);

df <- read.table(text =
"ID date Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2018-01-12 NA
193sd 2017-12-11 1", header = T, stringsAsFactors = F)

dt <- as.data.table(df)
dt[, date := as.Date(date)]
result <- dt[, .SD[date >= max(date) %m-% months(10)], by = ID][, .(customMean = sum(Stock, na.rm = T)/10), by = ID] #calculate the mean stock as you described for entries in date that are within 10 months from the last date for each ID
## ID customMean
##1: 193sd 0.5





share|improve this answer



















  • 1




    This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
    – Programmer Man
    Nov 19 at 12:30










  • inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
    – Patrik_P
    Nov 19 at 12:35










  • I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
    – Programmer Man
    Nov 19 at 13:23












  • please tell me something sooon thank you!
    – Programmer Man
    Nov 19 at 13:24










  • can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
    – Patrik_P
    Nov 19 at 13:24











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373964%2fmean-of-the-values-that-have-less-than-10-months-in-stock%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
1
down vote



accepted










With the packages data.table and lubridate you could do the followin'



library(data.table); library(lubridate);

df <- read.table(text =
"ID date Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2018-01-12 NA
193sd 2017-12-11 1", header = T, stringsAsFactors = F)

dt <- as.data.table(df)
dt[, date := as.Date(date)]
result <- dt[, .SD[date >= max(date) %m-% months(10)], by = ID][, .(customMean = sum(Stock, na.rm = T)/10), by = ID] #calculate the mean stock as you described for entries in date that are within 10 months from the last date for each ID
## ID customMean
##1: 193sd 0.5





share|improve this answer



















  • 1




    This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
    – Programmer Man
    Nov 19 at 12:30










  • inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
    – Patrik_P
    Nov 19 at 12:35










  • I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
    – Programmer Man
    Nov 19 at 13:23












  • please tell me something sooon thank you!
    – Programmer Man
    Nov 19 at 13:24










  • can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
    – Patrik_P
    Nov 19 at 13:24















up vote
1
down vote



accepted










With the packages data.table and lubridate you could do the followin'



library(data.table); library(lubridate);

df <- read.table(text =
"ID date Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2018-01-12 NA
193sd 2017-12-11 1", header = T, stringsAsFactors = F)

dt <- as.data.table(df)
dt[, date := as.Date(date)]
result <- dt[, .SD[date >= max(date) %m-% months(10)], by = ID][, .(customMean = sum(Stock, na.rm = T)/10), by = ID] #calculate the mean stock as you described for entries in date that are within 10 months from the last date for each ID
## ID customMean
##1: 193sd 0.5





share|improve this answer



















  • 1




    This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
    – Programmer Man
    Nov 19 at 12:30










  • inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
    – Patrik_P
    Nov 19 at 12:35










  • I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
    – Programmer Man
    Nov 19 at 13:23












  • please tell me something sooon thank you!
    – Programmer Man
    Nov 19 at 13:24










  • can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
    – Patrik_P
    Nov 19 at 13:24













up vote
1
down vote



accepted







up vote
1
down vote



accepted






With the packages data.table and lubridate you could do the followin'



library(data.table); library(lubridate);

df <- read.table(text =
"ID date Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2018-01-12 NA
193sd 2017-12-11 1", header = T, stringsAsFactors = F)

dt <- as.data.table(df)
dt[, date := as.Date(date)]
result <- dt[, .SD[date >= max(date) %m-% months(10)], by = ID][, .(customMean = sum(Stock, na.rm = T)/10), by = ID] #calculate the mean stock as you described for entries in date that are within 10 months from the last date for each ID
## ID customMean
##1: 193sd 0.5





share|improve this answer














With the packages data.table and lubridate you could do the followin'



library(data.table); library(lubridate);

df <- read.table(text =
"ID date Stock
193sd 2016-03-23 12
193sd 2017-09-12 1
193sd 2018-09-11 4
193sd 2018-01-12 NA
193sd 2017-12-11 1", header = T, stringsAsFactors = F)

dt <- as.data.table(df)
dt[, date := as.Date(date)]
result <- dt[, .SD[date >= max(date) %m-% months(10)], by = ID][, .(customMean = sum(Stock, na.rm = T)/10), by = ID] #calculate the mean stock as you described for entries in date that are within 10 months from the last date for each ID
## ID customMean
##1: 193sd 0.5






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 12:35

























answered Nov 19 at 12:03









Patrik_P

1,90221225




1,90221225








  • 1




    This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
    – Programmer Man
    Nov 19 at 12:30










  • inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
    – Patrik_P
    Nov 19 at 12:35










  • I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
    – Programmer Man
    Nov 19 at 13:23












  • please tell me something sooon thank you!
    – Programmer Man
    Nov 19 at 13:24










  • can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
    – Patrik_P
    Nov 19 at 13:24














  • 1




    This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
    – Programmer Man
    Nov 19 at 12:30










  • inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
    – Patrik_P
    Nov 19 at 12:35










  • I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
    – Programmer Man
    Nov 19 at 13:23












  • please tell me something sooon thank you!
    – Programmer Man
    Nov 19 at 13:24










  • can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
    – Patrik_P
    Nov 19 at 13:24








1




1




This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
– Programmer Man
Nov 19 at 12:30




This looks good but I get error Error in sum("Stock") : invalid 'type' (character) of argument I believe because I have NAs
– Programmer Man
Nov 19 at 12:30












inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
– Patrik_P
Nov 19 at 12:35




inn order to get sum excluding NAs you should do sum(..., na.rm = T) as you can see now in the edit
– Patrik_P
Nov 19 at 12:35












I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
– Programmer Man
Nov 19 at 13:23






I just checked and result for a certian ID given 10 months has 120 records, so doing the mean to those 120 records should be different ??
– Programmer Man
Nov 19 at 13:23














please tell me something sooon thank you!
– Programmer Man
Nov 19 at 13:24




please tell me something sooon thank you!
– Programmer Man
Nov 19 at 13:24












can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
– Patrik_P
Nov 19 at 13:24




can you provide more data? Make dput of first 100 rows of your data and paste it above into an edit
– Patrik_P
Nov 19 at 13:24


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373964%2fmean-of-the-values-that-have-less-than-10-months-in-stock%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]