首页 > 解决方案 > Dividing column values in group and finding standard deviation of that group

问题描述

enter image description here

I have a data frame as given.

The image contains two columns NAME and RANGE. range have values starting from 50000 to 70000 I want to dived Range in the group of 2000 like from 5000 to 52000 whichever ever value comes that should fall in that group and then I want to find the standard deviation of that group.

I was using the following code

tally(group_by(df,RANGE=cut(RANGE,breaks = seq(50000,70000,by=2000,))) %>%
 ungroup() %>% 
  spread(RANGE,n,fill = 0)

but I am not able to calculate S.d from this

I want my output as follow

RANGE   FREQ S.D
50K-52K 10   1.2
52K-54K 5    0.8
....
...
68K-70K 4    2

标签: r

解决方案


您可以尝试cut RANGE分组,然后参加sd每个小组。

library(dplyr)

df %>%
  group_by(group = cut(RANGE, breaks = seq(50000,70000,by=2000))) %>%
  summarise(sd = sd(RANGE), 
            Freq = n())

或类似使用base Raggregate

df$groups <- cut(df$RANGE,breaks = seq(50000,70000,by=2000))
aggregate(RANGE~groups, df, function(x) c(sd(x), length(x)))

推荐阅读