将列从数据框划分为另一个

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (6)

我有2个数据框,我试图相互划分,但它不适合我。两个数据帧都是8 x 3,第一列的数据帧相同,两个数据帧的列名也相同

bal_tier[,c(1, 3:4)]
# A tibble: 8 x 3
# Groups:   hierachy_level2 [8]
  hierachy_level2 `201804` `201904`
  <chr>              <dbl>    <dbl>
1 CS                   239     250 
2 FNZ                   87      97 
3 OPS                 1057    1136.
4 P&T                  256     279 
5 R&A                  520     546 
6 SPE                  130     136.
7 SPP                   67      66 
8 TUR                   46      69 



    dput(bal_tier[,c(1, 3:4)])
structure(list(hierachy_level2 = c("CS", "FNZ", "OPS", "P&T", 
"R&A", "SPE", "SPP", "TUR"), `201804` = c(239, 87, 1057, 256, 
520, 130, 67, 46), `201904` = c(250, 97, 1136.5, 279, 546, 136.5, 
66, 69)), row.names = c(NA, -8L), groups = structure(list(hierachy_level2 = c("CS", 
"FNZ", "OPS", "P&T", "R&A", "SPE", "SPP", "TUR"), .rows = list(
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))


tier_leavers[,c(1, 3:4)]
# A tibble: 8 x 3
# Groups:   hierachy_level2 [8]
  hierachy_level2 `201804` `201904`
  <chr>              <dbl>    <dbl>
1 CS                    32       47
2 FNZ                    1       11
3 OPS                   73       76
4 P&T                   48       33
5 R&A                   41       33
6 SPE                   28       30
7 SPP                   10       12
8 TUR                    2        3

    dput(tier_leavers[,c(1, 3:4)])
structure(list(hierachy_level2 = c("CS", "FNZ", "OPS", "P&T", 
"R&A", "SPE", "SPP", "TUR"), `201804` = c(32, 1, 73, 48, 41, 
28, 10, 2), `201904` = c(47, 11, 76, 33, 33, 30, 12, 3)), row.names = c(NA, 
-8L), groups = structure(list(hierachy_level2 = c("CS", "FNZ", 
"OPS", "P&T", "R&A", "SPE", "SPP", "TUR"), .rows = list(1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

这样做可以满足我的需求:

    bal_tier[,1]
# A tibble: 8 x 1
# Groups:   hierachy_level2 [8]
  hierachy_level2
  <chr>          
1 CS             
2 FNZ            
3 OPS            
4 P&T            
5 R&A            
6 SPE            
7 SPP            
8 TUR   

  (tier_leavers[,c(3:4)] / bal_tier[,c(3:4)])
      201804     201904
1 0.13389121 0.18800000
2 0.01149425 0.11340206
3 0.06906339 0.06687198
4 0.18750000 0.11827957
5 0.07884615 0.06043956
6 0.21538462 0.21978022
7 0.14925373 0.18181818
8 0.04347826 0.04347826

但是当我把它组合在一起时,我最终得到了这个:

    cbind(bal_tier[,1], tier_leavers[,c(3:4)] / bal_tier[,c(3:4)])
       [,1]        [,2]     
201804 Character,8 Numeric,8
201904 Character,8 Numeric,8

我在这里理解错了什么?

提问于
用户回答回答于

这是使用tidyverse的解决方案

nme <- c("A","B","C","D","E")
yr_1 <- round(10*runif(n=5,min=0,max=10),0)
yr_2 <- round(10*runif(n=5,min=0,max=10),0)

data_1 <- data.frame(nme,yr_1,yr_2) 

yr_1 <- round(10*runif(n=5,min=0,max=10),0)
yr_2 <- round(10*runif(n=5,min=0,max=10),0)

data_2 <- data.frame(nme,yr_1,yr_2) 

data_divide <- data_1 %>% 
  left_join(data_2,by="nme") %>% 
  mutate(
    result_1=yr_1.x/yr_1.y,
    result_2=yr_2.x/yr_2.y
    )
用户回答回答于

我最终做的就是作弊,但我从宙斯的回答中得到了一个线索:

a <- bal_tier[, 1]
b <- tier_leavers[,c(3:4)] / bal_tier[,c(3:4)]
tier_to <- data.frame(a, b)
tier_to

> tier_to
  hierachy_level2    X201804    X201904
1              CS 0.13389121 0.18800000
2             FNZ 0.01149425 0.11340206
3             OPS 0.06906339 0.06687198
4             P&T 0.18750000 0.11827957
5             R&A 0.07884615 0.06043956
6             SPE 0.21538462 0.21978022
7             SPP 0.14925373 0.18181818
8             TUR 0.04347826 0.04347826

所属标签

可能回答问题的人

  • 西风

    renzha.net · 站长 (已认证)

    7 粉丝1 提问9 回答
  • 四无君

    0 粉丝0 提问3 回答
  • o o

    3 粉丝490 提问2 回答
  • Dingda

    Dingda · 站长 (已认证)

    4 粉丝0 提问2 回答

扫码关注云+社区

领取腾讯云代金券