提问人:Gabrielle Macklin 提问时间:11/17/2023 更新时间:11/17/2023 访问量:40
基于命名向量填充数据框列
Filling in a data frame column based on a named vector
问:
我有两个向量,其中一个我用来命名另一个
vector <- c("Apple","Banana","Cucumber","Durian")
name <- c("A","B","C","D")
names(vector) <- name
我有一个正在填写的数据框
df <- data.frame("Fruit" = c("Banana","Durian","Apple","Cucumber"),
"Price" = c("$4.00", "$2.00","$1.50","$1.00"))
我想创建第三列“label”,它将根据向量中的名称进行填充。看起来像这样
Fruit Price Label
1 Banana $4.00 B
2 Durian $2.00 D
3 Apple $1.50 A
4 Cucumber $1.00 C
任何建议将不胜感激!(任何使用 tidyverse 的东西都会有所帮助!
答:
2赞
Mark
11/17/2023
#1
使用基础 R:
df$Label <- names(vector)[match(df$Fruit, vector)]
match()
在 vector 中找到变量的索引,然后我们从 names(vector) 中获取该元素。Fruit
使用 tidyverse,您可以使用相同的代码,只需替换为 .df$Fruit
Fruit
1赞
Sash Sinha
11/17/2023
#2
您可以使用 dplyr
:
library(dplyr)
vector <- c("Apple","Banana","Cucumber","Durian")
name <- c("A","B","C","D")
names(vector) <- name
df <- data.frame("Fruit" = c("Banana","Durian","Apple","Cucumber"),
"Price" = c("$4.00", "$2.00","$1.50","$1.00"))
df <- df %>%
left_join(data.frame(Fruit = vector, Label = names(vector)), by = "Fruit")
print(df)
输出:
Fruit Price Label
1 Banana $4.00 B
2 Durian $2.00 D
3 Apple $1.50 A
4 Cucumber $1.00 C
下一个:按降序排列 y 轴
评论
readr::parse_number(df$Price)