I have a data set that look like the following: http://i.imgur.com/OdiLf4t.png
Name | State | Zipcode | County_name | average payment | Frequency
My desired output would be to group by State and have the Median payment using the average payment and Frequency columns.
I know how to do this for the overall dataset
median(rep(Clean$medicare_average_payment, Clean$Frequency))
but not sure how to do this by State Thank you
We can try with dplyr
library(dplyr)
Clean1 <- Clean[rep(1:nrow(Clean), Clean$Frequency),]
Clean1 %>%
group_by(State) %>%
summarise(Median = median(medicare_average_payment))
Or using data.table
library(data.table)
setDT(Clean)[, .(Median = median(rep(medicare_average_payment, Frequency))) , State]
You can use by
to do split the data frame and perform this function on each piece:
by(Clean, Clean$State,
FUN=function(x) median(rep(x$medicare_average_payment, x$Frequency))
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.