如何从一个列引用其他列的名称并创建一个新列
How to refer from a column to names of other columns and create a new column
我有一个table比如
+---------+---------+--------+--------+--------+
| Product | Classif | Type 1 | Type 2 | Type 3 |
+---------+---------+--------+--------+--------+
| a | Type 1 | 2 | 6 | 8 |
| b | Type 2 | 3 | 9 | 11 |
| c | Type 3 | 5 | 10 | 15 |
+---------+---------+--------+--------+--------+
我有一份产品清单和它们的分类。产品和分类之间的匹配足以确定它们的价格(在第 3 至 5 列中)。
我想要一个新的列,根据其类型显示每个产品的价格,例如:
+---------+---------+--------+--------+--------+-------+
| Product | Classif | Type 1 | Type 2 | Type 3 | Price |
+---------+---------+--------+--------+--------+-------+
| a | Type 1 | 2 | 6 | 8 | 2 |
| b | Type 2 | 3 | 9 | 11 | 9 |
| c | Type 3 | 5 | 10 | 15 | 15 |
+---------+---------+--------+--------+--------+-------+
其中程序比较列 classif 的值,并从相应的列中取值。
这个有用吗?
library(data.table)
df <- data.table(Product = c('a', 'b', 'c'),
Classif = c('Type 1', 'Type 2', 'Type 3'),
`Type 1` = c(2, 3, 5),
`Type 2` = c(6,9,10),
`Type 3` = c(8,11,15)
)
df2 <- df[,`:=`(
Price = case_when(
Classif == 'Type 1' ~ `Type 1`,
Classif == 'Type 2' ~ `Type 2`,
Classif == 'Type 3' ~ `Type 3`
)
)]
可以达到你要找的东西,先将你的数据整形为多头,然后计算比较以获得价格,以便与 left_join()
一起加入。这里的代码使用 tidyverse
函数:
library(tidyverse)
#Code
df2 <- df %>% left_join(df %>% pivot_longer(-c(Product,Classif)) %>%
mutate(Price=ifelse(Classif==name,value,NA)) %>%
filter(!is.na(Price)) %>% select(-c(name,value)))
输出:
Product Classif Type 1 Type 2 Type 3 Price
1 a Type 1 2 6 8 2
2 b Type 2 3 9 11 9
3 c Type 3 5 10 15 15
使用了一些数据:
#Data
df <- structure(list(Product = c("a", "b", "c"), Classif = c("Type 1",
"Type 2", "Type 3"), `Type 1` = c(2, 3, 5), `Type 2` = c(6, 9,
10), `Type 3` = c(8, 11, 15)), row.names = c(NA, -3L), class = "data.frame")
我有一个table比如
+---------+---------+--------+--------+--------+
| Product | Classif | Type 1 | Type 2 | Type 3 |
+---------+---------+--------+--------+--------+
| a | Type 1 | 2 | 6 | 8 |
| b | Type 2 | 3 | 9 | 11 |
| c | Type 3 | 5 | 10 | 15 |
+---------+---------+--------+--------+--------+
我有一份产品清单和它们的分类。产品和分类之间的匹配足以确定它们的价格(在第 3 至 5 列中)。 我想要一个新的列,根据其类型显示每个产品的价格,例如:
+---------+---------+--------+--------+--------+-------+
| Product | Classif | Type 1 | Type 2 | Type 3 | Price |
+---------+---------+--------+--------+--------+-------+
| a | Type 1 | 2 | 6 | 8 | 2 |
| b | Type 2 | 3 | 9 | 11 | 9 |
| c | Type 3 | 5 | 10 | 15 | 15 |
+---------+---------+--------+--------+--------+-------+
其中程序比较列 classif 的值,并从相应的列中取值。
这个有用吗?
library(data.table)
df <- data.table(Product = c('a', 'b', 'c'),
Classif = c('Type 1', 'Type 2', 'Type 3'),
`Type 1` = c(2, 3, 5),
`Type 2` = c(6,9,10),
`Type 3` = c(8,11,15)
)
df2 <- df[,`:=`(
Price = case_when(
Classif == 'Type 1' ~ `Type 1`,
Classif == 'Type 2' ~ `Type 2`,
Classif == 'Type 3' ~ `Type 3`
)
)]
可以达到你要找的东西,先将你的数据整形为多头,然后计算比较以获得价格,以便与 left_join()
一起加入。这里的代码使用 tidyverse
函数:
library(tidyverse)
#Code
df2 <- df %>% left_join(df %>% pivot_longer(-c(Product,Classif)) %>%
mutate(Price=ifelse(Classif==name,value,NA)) %>%
filter(!is.na(Price)) %>% select(-c(name,value)))
输出:
Product Classif Type 1 Type 2 Type 3 Price
1 a Type 1 2 6 8 2
2 b Type 2 3 9 11 9
3 c Type 3 5 10 15 15
使用了一些数据:
#Data
df <- structure(list(Product = c("a", "b", "c"), Classif = c("Type 1",
"Type 2", "Type 3"), `Type 1` = c(2, 3, 5), `Type 2` = c(6, 9,
10), `Type 3` = c(8, 11, 15)), row.names = c(NA, -3L), class = "data.frame")