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
- SORT the dt$date from newest to oldest
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
check if M<=10
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
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
add a comment |
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
- SORT the dt$date from newest to oldest
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
check if M<=10
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
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
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
add a comment |
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
- SORT the dt$date from newest to oldest
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
check if M<=10
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
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
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
- SORT the dt$date from newest to oldest
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
check if M<=10
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
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
r data.table
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
add a comment |
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
add a comment |
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
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 dosum(..., na.rm = T)
as you can see now in the edit
– Patrik_P
Nov 19 at 12:35
I just checked andresult
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
|
show 2 more comments
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
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 dosum(..., na.rm = T)
as you can see now in the edit
– Patrik_P
Nov 19 at 12:35
I just checked andresult
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
|
show 2 more comments
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
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 dosum(..., na.rm = T)
as you can see now in the edit
– Patrik_P
Nov 19 at 12:35
I just checked andresult
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
|
show 2 more comments
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
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
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 dosum(..., na.rm = T)
as you can see now in the edit
– Patrik_P
Nov 19 at 12:35
I just checked andresult
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
|
show 2 more comments
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 dosum(..., na.rm = T)
as you can see now in the edit
– Patrik_P
Nov 19 at 12:35
I just checked andresult
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
|
show 2 more comments
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%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
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
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