添加行值然后在数据帧之间划分

Adding row values then dividing between data frames

我的第一个数据框是这样的:

ID 2016 2017
1  5    6
2  15   20
3  10   10

第二个数据框相同,但值不同:

ID 2016 2017
1  20   30
2  50   40
3  10   15

我想在每个 table 中添加 ID 号 1 和 3,然后在新数据帧中将第一个数据帧除以第二个数据帧。我还想将第一个 table 中的 ID 2 除以第二个中的 ID 2。最后,我希望新数据框的行名包含这些计算结果,因此:

Type 2016 2017
A    0.5  0.36
B    0.3  0.5

A 行是 ID 1 和 3 的结果,而 B 行是 ID 2 的结果。

我不确定这是最优雅的解决方案:


library(dplyr)
library(purrr)
library(tibble)

df1 <- structure(list(`2016` = c(5L, 15L, 10L), `2017` = c(6L, 20L, 10L)), class = "data.frame", row.names = c(NA, -3L))

df2 <- structure(list(`2016` =  c(20L, 50L, 10L), `2017` = c(30L, 40L, 15L)), class = "data.frame", row.names = c(NA, -3L))

# add ID to each dataframe

df1  %>%  
  rowid_to_column(var = "ID")
#>   ID 2016 2017
#> 1  1    5    6
#> 2  2   15   20
#> 3  3   10   10

要根据您的要求创建第三个数据框,我不确定我们是否需要 ID 列,所以...


#A little function to prepare each data frame

df_Type <- function(x){

  x %>% 
  mutate(Type = c("A", "B", "A")) %>% 
  group_by(Type) %>% 
  summarise_all(sum)

}


# the function could be place in the list below to avoid additional objects but it makes it clear what is happening

df1_Type <- df_Type(df1)

df2_Type <- df_Type(df2)

> df2_Type
# A tibble: 2 x 3
  Type  `2016` `2017`
  <chr>  <int>  <int>
1 A         30     45
2 B         50     40

#dividing one data frame by the other

list(select_if(df1_Type, is.numeric),
     select_if(df2_Type, is.numeric)) %>% 
pmap_dfr(function(x, y) x / y) %>% 
bind_cols(df1_Type[, 1]) %>% 
  select(Type, everything())

#> # A tibble: 2 x 3
#>   Type  `2016` `2017`
#>   <chr>  <dbl>  <dbl>
#> 1 A        0.5  0.356
#> 2 B        0.3  0.5

reprex package (v0.3.0)

于 2020-05-21 创建

为此,我们还可以使用base R。获取两个数据集的行子集的 colSums,除以每个数据集的第 2 行的除法 rbind

cbind(Type = c('A', 'B'), rbind.data.frame(colSums(df1[-2, 
     -1])/colSums(df2[-2, -1]), df1[2, -1]/df2[2, -1]))
#   Type 2016      2017
#1    A  0.5 0.3555556
#2    B  0.3 0.5000000

此处,子集是针对具有索引

的行和列完成的
df2[-2, -1] 

表示,我们删除了第二行和第一列。索引是行,列。如果它是正数,那么我们将保留 rows/columns。在这里,那些 rows/columns 被删除。

数据

df1 <- structure(list(ID = 1:3, `2016` = c(5L, 15L, 10L), `2017` = c(6L, 
20L, 10L)), class = "data.frame", row.names = c(NA, -3L))

df2 <- structure(list(ID = 1:3, X2016 = c(20L, 50L, 10L), X2017 = c(30L, 
40L, 15L)), class = "data.frame", row.names = c(NA, -3L))