问题
I am fairly new to R/Rstudio and I am still learning how to do certain operations. I have the following data set. For columns I have Operating Region, type of element(CA,OBU), sub-element and Net Revenue. Currently the data is quite big(50 000 rows) and I want to get a summary of Operating region by element,sub-element and NR. Example
Operating Region Element Sub-Element NR
Asia CA CA123 50 000
America OBU EFK456 35 000
Could someone please guide me on how to accomplish this? Any relevant readings/examples would be much appreciated.
回答1:
Using the data below to return the data frame object "data," you can use the dplyr
package to organize results in many different ways. Here is one example:
data <- data.frame("OperatingRegion" = c("Asia", "America"), "Region" = c("CA", "OBU"), "Element" = c("CA123", "EFK456"), "SubElement" = c(50000, 35000))
require(dplyr)
results <- data %.%
group_by(OperatingRegion) %.%
summarise(SubE = sum(SubElement, na.rm = TRUE))
Source: local data frame [2 x 2]
OperatingRegion SubE
1 America 35000
2 Asia 50000
After loading the package, you provide dplyr the data frame and then, using the special operators %.% or %>%, group_by whatever single or multiple variables you want. Then, call summarise
to create sums, medians, averages or whatever computation you want.
来源:https://stackoverflow.com/questions/25968137/r-data-subset-restructuring