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

时间:2019-05-20 03:59:40

标签: r dataframe

我有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

我在这里理解错了什么?

2 个答案:

答案 0 :(得分:0)

这是使用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
    )

答案 1 :(得分:0)

我最终所做的事情感觉像是在作弊,但是我从宙斯的答案中得到了一个线索:

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