![](/img/trans.png)
[英]Conditionally take value from column1 if the column1 name == first(value) from column2 BY GROUP
[英]How to assign a name to a new column that follows the structure column1*column2 in R?
我有一个由 Date、Brand1Index、Brand1Volume、Brand2Index、Brand2Volume 等列组成的数据框。我现在想创建一个新的数据框来存储 (Brand1Index,Brand1volume), (Brand2Index, Brand2Volume) --> New数据框中的每个品牌都有一列来自另一个数据框。 我设法计算了每个品牌的产品,并将它们放入新的数据框中。 但是,我希望新数据框的列以我使用的函数命名。 例如,当新数据框的 Column1 表示 Brand1Index x Brand1Volume 时,它应该这样命名。 到目前为止,这些列被命名为“5 x 18.8”等。这些是列的值......下面是我的代码片段:
df <- NULL
j <- 1
for(i in seq(2, 10, 2)) {
df[j] <- data[i]*data[i+1]
j <- j+1
}
df <- data.frame(df)
假设您有一个数据框,如:
df
# date brand1Index brand1Volume brand2Index brand2Volume
# <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 4 1 1 3 3
# 2 5 2 2 2 2
# 3 6 3 3 1 1
你运行你的代码并得到:
df2
# wrongName1 wrongName2
# <dbl> <dbl>
# 1 1 9
# 2 4 4
# 3 9 1
然后您可以使用基于 tidyverse 的以下解决方案:
library(dplyr)
library(tibble)
names <- df1 %>%
colnames() %>%
enframe() %>%
filter(value != 'date') %>%
mutate(group = cumsum(+(row_number() %% 2 == 1))) %>%
group_by(group) %>%
mutate(newName = str_c(value, collapse = '_x_')) %>%
ungroup() %>%
distinct(newName) %>%
pull()
df2 %>% rename_with(~ names, .cols = colnames(.))
# brand1Index_x_brand1Volume brand2Index_x_brand2Volume
# <dbl> <dbl>
# 1 1 9
# 2 4 4
# 3 9 1
一次运行一行,您将看到第一个mutate()
创建了一个新变量,该变量将需要放在一个名称中的名称配对。 然后将配对的名称分组并放在一起。 这些操作为我们提供了使用distinct()
删除并通过pull()
转换为向量的重复项。
在向量中以正确的顺序使用正确的变量名称确保我们可以在最后一行正确重命名df2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.