Sum values for a specific period [duplicate] - r

This question already has answers here:
R data.table sum of group subset using dates
(3 answers)
Count rows since a date condition is met
(2 answers)
Count number of rows in window around timestamp
(3 answers)
Closed 4 years ago.
I have some IDs, dates and Values.
I want to sum values for each ID for the last 10 days.
This code can reproduce my dataset:
date<-as.Date(c("05/03/2017","05/10/2017","05/14/2017","05/19/2017","05/22/2017"),format="%m/%d/%Y")
value<-rpois(20,50)
id<-NULL
for(i in 1:4){id<-c(id,rep(i,5))}
mydata<-data.frame(id,date=rep(date,4),value)
So, the output would be the dataset above with a new column. This column would have the sum of values of lasts 10 days.
Thanks in advance.

Related

Get the average for each 4 rows [duplicate]

This question already has answers here:
How to get the sum of each four rows of a matrix in R
(3 answers)
Average of n rows
(1 answer)
Closed 8 months ago.
I have a column with hundreds of values, I want to get the average for each 4 rows, then I move to another four rows, etc. How could I write that loop in R studio, I attached here a simple example, what should I do if I have NA values, how to consider those values as zeros?

Is there an R Function whrer i can combine multiple rows? [duplicate]

This question already has answers here:
Group by multiple columns and sum other multiple columns
(7 answers)
How to sum a variable by group
(18 answers)
Closed 2 years ago.
I have this table:
I want to combine the rows, so that the values off the variables are in one row from the same month.

How to create a table with sum of a datapoints in a given month in R [duplicate]

This question already has answers here:
Get monthly means from dataframe of several years of daily temps
(3 answers)
Closed 5 years ago.
So I have a dataset with 5000 data points over 3 years, each row is a unique order (in dd/mm/yyy). How can I manipulate this to a return the sum of each data point per month? So I can then plot a graph with x amount of sales in june '14, x amount in july '14 etc...
use substring(df$Date, 4) to substring the date column by removing dd/
Aggregate with aggregate(df, by = df$Date, sum)

The most efficient way to calculate numbero of unique values in vector [duplicate]

This question already has answers here:
How to count the number of unique values by group? [duplicate]
(1 answer)
Unique values in each of the columns of a data frame
(9 answers)
Closed 5 years ago.
Is there better way to compute this instead of:
length(unique(vector))
let's assume that we donno what class is the vector.
You can use
library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)

How can I select the smallest value from the subset of a subset? [duplicate]

This question already has answers here:
Select rows with min value by group
(10 answers)
Closed 9 years ago.
I have a dataframe with three columns: Batch, Trial, Time.
Five Trials (0-4) are ran for each Batch number.
I want to pull out the row with the smallest time from each Batch and put them into a new dataframe.
I'm not sure where to start.
Assuming the dataframe as df.
Try
df.new <- df[ df$Time == ave(df$Time, df$Batch, FUN=min), ])

Resources