Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit dataframe and calculate averages for data subsets in R
    primarykey
    data
    text
    <p>I have this data frame in R:</p> <pre><code>steps day month 4758 Tuesday December 9822 Wednesday December 10773 Thursday December </code></pre> <p>I want to iterate over the data frame and apply a function to the steps column based on the value in the month column. I'm trying to work out the average number of steps per weekday for each month.</p> <p>I want to output to a new data frame like so where the week days repeat but I only have the average values per day:</p> <pre><code>average.steps day month 4500 Tuesday December 9000 Wednesday December 1000 Thursday December </code></pre> <p>I can work out how to work out the averages for the data frame as a whole, but want to use a for loop to apply it just for step values from the same month.</p> <pre><code>avgsteps &lt;- ddply(DATA, "day", summarise, msteps = mean(steps)) </code></pre> <p>My basic idea for the for function was:</p> <pre><code>f &lt;- function(m in month) {ddply(DATA, "day", summarise, msteps = mean(steps))} </code></pre> <p>But it won't process it and throws the error:</p> <pre><code>Error: unexpected 'in' in "f &lt;- function(m in" </code></pre> <p>Any help would be greatly appreciated!</p> <p>EDIT:</p> <p>SO I've tried @agstudy's suggested fix (below) and it gets the right data structure (single value for each weekday for each month), but the value assigned to each day is identical. I'm a bit confused what could be going wrong.</p> <pre><code>steps.month.day.avg &lt;- ddply(steps.month.day, .(fitbit.day,fitbit.month), summarise, msteps = mean(steps)) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload