簡體   English   中英

R 代碼按 % 拆分值,然后為每個新的 % 值分配一個新類別

[英]R code split value by % then assign each new % value a new category

我還沒有找到一種方法來做到這一點,所以詢問是否有更簡單的方法來做到這一點。 這是數據集的示例:

    Revenue      Product    New Code
1  223,220.00     Apple
2  386,640.40     Apple            
3  19,891.95      Apple   

我需要獲取每個收入行,按不同百分比分配收入,然后將每個百分比分配給一個新代碼。

舉個例子,

對於 Apple,收入應通過以下方式分配:

  • 0.45 轉到“A”。
  • 0.50 轉到“B”。
  • 0.05 轉到“C”。

因此,數據集中的第一個值,Revenue =223,220.00,應該分配如下:

    Revenue      Product    New Code
1  100,449        Apple       A
2  111,610        Apple       B         
3  11,161         Apple       C

這將增加行數。

我嘗試使用此代碼,但想知道是否有更簡單的方法可以做到這一點?

    #
# libraries
#
library(dplyr)
#
# load data
#
my_data <- read.csv('sales_data_to_reclassify.csv', stringsAsFactors = FALSE)
#
# get total category revenue
#
Apple_revenue <- sum(my_data[substr(my_data$product, 1, 4) == 'Apple', 'Revenue'])
Apple_rows <- which(substr(my_data$product, 1, 4) == 'Apple')
#
# set the splits
#
splits <- list(A = 0.45,
               B = 0.50,
               C = 0.05)
#
# apply the splits at row level
#
for (i in Apple_rows) {
  #
  # revenue for this row in the original data
  #
  row_revenue = my_data[i, 'Revenue']
  for (label in names(splits)) {
    #
    # grab the row
    #
    new_row <- my_data[i, ]
    #
    # calculate the revenue for this split
    # and update the new row
    #    
    new_row$Revenue <- row_revenue * splits[[label]]
    #
    # assign the label
    #
    new_row$New.Code <- label
    #
    # build a temporary data frame to hold the new rows
    #
    if (label == names(splits)[1]) {
      new_rows <- new_row
    } else {
      new_rows <- rbind(new_rows, new_row)
    }
    rownames(my_data) <- NULL
    Apple_rows <- which(substr(my_data$product, 1, 4) == 'Apple')
  }
  #
  # drop the original row
  #
  my_data <- my_data[-i, ]
  #
  # add in the new rows
  #
  my_data <- rbind(my_data, new_rows)
}
#
# test revenue
#
Apple_new_revenue <-  sum(my_data[substr(my_data$product, 1, 4) == 'Apple', 'Revenue'])

這是一個非常簡單的dplyr解決方案:

df %>% 
  filter(Product %in% c("Apple", "Microsoft", "Samsung") %>%
  mutate(A = Revenue * 0.45,
         B = Revenue * 0.50,
         C = Revenue * 0.05) %>% 
  select(-Revenue) %>% 
  pivot_longer(-Product, values_to = "Revenue") %>% 
  rename(`New Code` = name) %>% 
  select(Revenue, Product, `New Code`)

這給了我們:

  Revenue Product `New Code`
    <dbl> <chr>   <chr>     
1 100449  Apple   A         
2 111610  Apple   B         
3  11161  Apple   C         
4 173988. Apple   A         
5 193320. Apple   B         
6  19332. Apple   C         
7   8951. Apple   A         
8   9946. Apple   B         
9    995. Apple   C   

這是一個更長但類似的base R解決方案:

# Remove commas from Revenue and convert to numeric
df$Revenue <- as.numeric(gsub(",", "", df$Revenue))

df <- subset(df, df$Product %in% c("Apple", "Microsoft", "Samsung"))

# Calculate percentage distributions
df$A <- df$Revenue * 0.45
df$B <- df$Revenue * 0.50
df$C <- df$Revenue * 0.05

# Reshape data to long
df <- reshape(df, 
        varying = c("A","B","C"),
        v.names = "Revenue",
        direction = "long")

# Sort by ID and recode values
df <- df[order(df$id),]
df$time[df$time == 1] <- "A"
df$time[df$time == 2] <- "B"
df$time[df$time == 3] <- "C"

# Drop ID column
df <- subset(df, select = -c(id))

# Rename 'time' to 'New Code'
names(df)[3] <- "New Code"

這給了我們:

       Revenue Product New Code
1: 100449.0000   Apple        A
2: 111610.0000   Apple        B
3:  11161.0000   Apple        C
4: 173988.1800   Apple        A
5: 193320.2000   Apple        B
6:  19332.0200   Apple        C
7:   8951.3775   Apple        A
8:   9945.9750   Apple        B
9:    994.5975   Apple        C

使用合並

# example data - updated to include more products.
my_data  <- read.table(text = "Revenue Product
223220.0 Apple
386640.4 Apple
19891.95 Pear", header = TRUE)

# define shares
splits <- data.frame(Product = rep(c("Apple", "Pear"), c(3, 3)),
                     NewCode = c("A", "B", "C", "X", "Y", "Z"),
                     Share = c(0.45, 0.50, 0.05, 0.2, 0.3, 0.5))

# merge and get revenue shares
res <- merge(my_data, splits, by = "Product")
res$RevenueShare <- res$Revenue * res$Share

res
#   Product   Revenue NewCode Share RevenueShare
# 1   Apple 223220.00       A  0.45   100449.000
# 2   Apple 223220.00       B  0.50   111610.000
# 3   Apple 223220.00       C  0.05    11161.000
# 4   Apple 386640.40       A  0.45   173988.180
# 5   Apple 386640.40       B  0.50   193320.200
# 6   Apple 386640.40       C  0.05    19332.020
# 7    Pear  19891.95       X  0.20     3978.390
# 8    Pear  19891.95       Y  0.30     5967.585
# 9    Pear  19891.95       Z  0.50     9945.975

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM