logo
down
shadow

repeated observations average per month


repeated observations average per month

Content Index :

repeated observations average per month
Tag : r , By : goffi
Date : November 24 2020, 04:01 AM

it helps some times This calculates the number of reciepts (rows) per houehold, per 4-week period
library(data.table)
setDT(df)

n_reciepts <- df[, .N, by = .(Household, period = floor(Week/4))]
#    Household period N
# 1:         1  50375 3
# 2:         2  50375 4
# 3:         3  50375 2
# 4:         1  50376 1
# 5:         3  50376 2
avg_n_reciepts <- n_reciepts[, .(avg_reciepts = mean(N)), by = Household]


#    Household avg_reciepts
# 1:         1            2
# 2:         2            4
# 3:         3            2
df[, .N, by = .(Household, period = floor(Week/4))
   ][, .(avg_reciepts = mean(N)), by = Household]


#    Household avg_reciepts
# 1:         1            2
# 2:         2            4
# 3:         3            2
library(dplyr)

df %>% 
  group_by(Household, period = floor(Week/4)) %>% 
  count %>% 
  group_by(Household) %>% 
  summarise(avg_reciepts = mean(n))

# # A tibble: 3 x 2
#   Household avg_reciepts
#       <dbl>        <dbl>
# 1         1            2
# 2         2            4
# 3         3            2

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Produce a precision weighted average among rows with repeated observations


Tag : r , By : Juan Pablo
Date : March 29 2020, 07:55 AM
I wish this helpful for you I like the plyr package for these sorts of problems. It should be functionally equivalent to aggregate, but I think it is nice and convenient to use. There are lots of examples and a great ~20 page intro to plyr on the website. For this problem, since the data starts as a data.frame and you want another data.frame on the other end, we use ddply()
library(plyr)
#f1()
ddply(example, "id", summarize, 
      newMean = weighted.mean(x=var1, 1/SE1, na.rm = TRUE),
      newSE = 1/sum(1/SE1, na.rm = TRUE)
      )
    id newmean   newSE
1  Bob  8.8982 0.91917
2 Jeff  4.6375 2.51690
3  Joe  7.8734 1.05064
4  Kim  7.1984 1.04829
5  Sam 11.1130 2.71324
6 Sara  9.8376 1.95649
library(data.table)
dt <- data.table(example, key="id")
#f2()
dt[, list(newMean = weighted.mean(var1, 1/SE1, na.rm = TRUE),
          newSE = 1/sum(1/SE1, na.rm = TRUE)),
   by = "id"]
library(rbenchmark)
#f1 = plyr, #f2 = data.table
benchmark(f1(), f2(), 
          replications = 1000,
          order = "elapsed",
          columns = c("test", "elapsed", "relative"))

      test elapsed relative
    2 f2()   3.580   1.0000
    1 f1()   6.398   1.7872

insert average month values in to another table but present month average values should prevet to pass to table


Tag : mysql , By : Shane
Date : March 29 2020, 07:55 AM
I hope this helps . This query works well. I want to prevent this month averages pass to avg_month_val1 how can i do it. , Maybe this does what you intent:
INSERT IGNORE INTO `clima_data`.`avg_month_val1`  ( `year` , `month` ,  
                                `evep` , `sunshine_hrs` , `rainfall` , 
                                `max_temp` , `min_temp` ) 
SELECT year(str_to_date(date, '%m/%d/%Y'))as year, 
       month(str_to_date(date, '%m/%d/%Y'))as month,
       round(avg(evep),2),
       round(Avg(sunshine_hrs),2),
       round(sum(rainfall),2),
       round(AVG(max_temp),2),
       round(avg(min_temp),2) 
FROM reg_data3
GROUP BY year(str_to_date(date, '%m/%d/%Y')),
         month(str_to_date(date, '%m/%d/%Y')) 
HAVING (year(str_to_date(date, '%m/%d/%Y')) <> year(CURRENT_TIMESTAMP)
     OR month(str_to_date(date, '%m/%d/%Y')) <> month(CURRENT_TIMESTAMP) )
ORDER BY 1 Desc;

How to identify repeated variables within observations?


Tag : r , By : James Dio
Date : March 29 2020, 07:55 AM
seems to work fine I'm new to R, I have a very long data set with presumably some repeated values (dates) in different variables, I want to assess whether two or more variables (if possible) are equal or not for each individual. , It seems like a job for apply. Here's a possible solution
mydata2 <- as.data.frame(t(apply(mydata, 1, function(x){temp <- unique(x); 
                                             c(temp, rep("", length(x) - length(temp)))})))
names(mydata2) <- names(mydata)
mydata2
#   Id      date1      date2 date3 date25
# 1  1 17/10/2002 25/01/2008             
# 2  2 13/04/2009                        
# 3  3 07/02/2008                        
# 4  4 24/11/2006 09/06/2010    

Get an average per month in h2 that is one average regardles of which year or month


Tag : sql , By : mckasty
Date : March 29 2020, 07:55 AM
wish of those help I think you almost got it the first time. You need to:
Add grouping by year/month into your original query, to get average per month. Perform a select on the result you already have and group it by truck, surrounding the first query with an extra select:
select truckcode, avg(avgPetrolQty) from (
  SELECT t.truckCode,
  COALESCE(year(orderDate),'Not Announced') as year,
  COALESCE(monthname(orderDate),'Not Announced') as month,
  IFNULL (avg(petrolQty),0) as avgPetrolQty
  from truck t left join orderz o
  on t.truckId = o.truckId
  group by t.truckCode, 
    COALESCE(year(orderDate),'Not Announced'),
    COALESCE(monthname(orderDate),'Not Announced')
) group by truckcode

summarize over repeated observations


Tag : r , By : SachinJadhav
Date : March 29 2020, 07:55 AM
To fix the issue you can do akrun's answer is more elegant, but as an alternative you can simply add the group variable to your group_by() call:
library(dplyr)

dat <- tibble(id = c(1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4), 
              group = c(1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0))

dat %>%
  group_by(id, group) %>%
  tally()

# A tibble: 4 x 3
# Groups:   id [4]
     id group     n
  <dbl> <dbl> <int>
1     1     1     3
2     2     0     4
3     3     1     1
4     4     0     5
dat2 <- tibble(id = c(1, 1, 1, 2, 2), group = c(1, 0, 0, 1, 0))

dat2 %>%
  group_by(id, group) %>%
  tally()

# A tibble: 4 x 3
# Groups:   id [2]
     id group     n
  <dbl> <dbl> <int>
1     1     0     2
2     1     1     1
3     2     0     1
4     2     1     1
Related Posts Related QUESTIONS :
  • Is there an R function for comparing rows in data.frame?
  • Changing linetype and line color with plot_model()
  • Update existing package on CRAN
  • Delete NA data ,but with certain condition in R
  • calculate number and names of similar sounding words from a data frame
  • Reset input fields of dynamically generated widgets through insertUI
  • How to get the coordinates that spits out min/max value from the function in R?
  • running t.test() on multiple columns to output tibble
  • Conditionally replace the values in columns to value in another column using dplyr
  • Add count as label to points in geom_count
  • Temporarily Disable Rprofile
  • Select certain region of column for lm
  • Convert multiple rows into one row depending on unique values in another column
  • Issues installing Plotly Dash for R
  • Is there an R function to retrieve values from a matrix of column names?
  • R;Too slow to overate loops for million vectors
  • How to optimize intersect of rows and columns in a matrix?
  • Format and export the output of Mann-Kendall test in R to excel from Rstudio
  • how to calculate cumsum with depreciation in a grouped dataframe?
  • reshape wide to long based on part of column name
  • How to get a hyperlink for the words in a description in an r dataframe?
  • shinymeta works locally but breaks when published to shinyapps.io
  • Deparse and (un)escape quotes
  • Regression table with clustered standard errors in R jupyter notebook?
  • Disaggregate quarterly data to daily data in R keeping values?
  • How to save output to console and file simultaneously in RStudio server?
  • Why does data.table j have a different environment when directly calling mget() vs calling mget() inside another functio
  • scale_fill_viridis_c color bar on a log scale
  • How to change the lab name corresponding to function in ggplot
  • R, filtering for an element in a list in a dataframe cell
  • Extracting only bottom temperature from 4d NetCDF file
  • How to add/wrap lines of text to .tex with .sh script
  • R - building new variables from sequenced data
  • Sum rows values one after the other
  • Nesting ifelse inside summarytools
  • How best to divide different levels of a factor by one another in dataframe in R?
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com