2

I'm grouping a data frame by the column "month", and then summarising the "users" column.

Using this code:

Count_Users_By_Month <- Users_By_Month %>% group_by(month) %>% 
  summarise(Users = length(unique(users)))

I get this, that i'm 100% sure it's correct:

     month       Users
1 Diciembre      4916
2 Noviembre      3527

Question 1: How to add a column showing the variation in "Diciembre" based on "Noviembre"?(In percentage %).

Need to create a colum for the variation month to month

The formula (pseudocode) is this one:

(DiciembreUsers-NoviembreUsers)/NoviembreUsers

** Of course the value for Noviembre would be clear cause there is no data from previous month (October).

I tried this code to do this, but get an error:

Count_Users_By_Month <- Users_By_Month %>% group_by(month) %>% 
  summarise(Users = length(unique(users))) %>%
  mutate(Variacion = (Count_Users_By_Month[1,2]-Count_Users_By_Month[2,2])/Count_Users_By_Month[2,2])

Error: not compatible with STRSXP

**Last edit:

Problem solved, Thanks @Khasha. See comments:

Changed "lag" for "lead".... it worked. Just added "lead" to the divison part to get the formula right.

mutate(variation=(Users-lead(Users))/lead(Users))
4

1 回答 1

1

这是原始数据框:

    month       Users
1 Diciembre      4916
2 Noviembre      3527

这是答案:

Count_Users_By_Month <- Users_By_Month %>% group_by(month) %>% 
                        summarise(Users = length(unique(users))) %>%
                        mutate(variation=(Users-lead(Users))/lead(Users))

需要调查“铅”是如何工作的。@Khashaa 的所有功劳,请在评论中查看他的答案。刚刚修改了公式,在除法部分加了“lead”得到正确答案

于 2015-01-08T06:01:12.447 回答