R 如何创建新列,将值与来自不同数据帧的标题相匹配
似乎太简单而不能失败:我有一个带有名称和值的数据框,我想创建一个新列,其中包含解释名称的标签。这些标签存在于单独的数据框中。因此,我试图使用dplyr的R 如何创建新列,将值与来自不同数据帧的标题相匹配,r,dplyr,mutate,R,Dplyr,Mutate,似乎太简单而不能失败:我有一个带有名称和值的数据框,我想创建一个新列,其中包含解释名称的标签。这些标签存在于单独的数据框中。因此,我试图使用dplyr的mutate()和pull(),但得到了一个错误 数据 库(TIBLE) 图书馆(dplyr) df 所以我要做的是转置你的标签查找文件,做一些抛光,然后右键连接你原来的df。如果您需要一个特定的列顺序,您可以对列顺序进行更多的调整,但它可以完成任务 所以我要做的是转置你的标签查找文件,做一些抛光,然后右键连接你原来的df。如果您需要一个特定的列
mutate()
和pull()
,但得到了一个错误
数据
库(TIBLE)
图书馆(dplyr)
df
所以我要做的是转置你的标签查找文件,做一些抛光,然后右键连接你原来的df。如果您需要一个特定的列顺序,您可以对列顺序进行更多的调整,但它可以完成任务
所以我要做的是转置你的标签查找文件,做一些抛光,然后右键连接你原来的df。如果您需要一个特定的列顺序,您可以对列顺序进行更多的调整,但它可以完成任务。这是否有效:
library(dplyr)
library(tidyr)
labels_tbl %>% pivot_longer(cols = everything()) %>%
select('animal' = name, 'label' = value) %>% inner_join(df)
Joining, by = "animal"
# A tibble: 2 x 3
animal label weight
<chr> <chr> <dbl>
1 dog a domesticated carnivore of the family Canidae 20
2 cat a domestic species of small carnivorous mammal 10
库(dplyr)
图书馆(tidyr)
标签%>%pivot\u更长(cols=everything())%%>%
选择('animal'=名称,'label'=值)%>%内部连接(df)
通过=“动物”加入
#一个tibble:2x3
动物标签重量
犬科家养食肉动物
2猫一种小型食肉哺乳动物的家养物种10
这是否有效:
library(dplyr)
library(tidyr)
labels_tbl %>% pivot_longer(cols = everything()) %>%
select('animal' = name, 'label' = value) %>% inner_join(df)
Joining, by = "animal"
# A tibble: 2 x 3
animal label weight
<chr> <chr> <dbl>
1 dog a domesticated carnivore of the family Canidae 20
2 cat a domestic species of small carnivorous mammal 10
库(dplyr)
图书馆(tidyr)
标签%>%pivot\u更长(cols=everything())%%>%
选择('animal'=名称,'label'=值)%>%内部连接(df)
通过=“动物”加入
#一个tibble:2x3
动物标签重量
犬科家养食肉动物
2猫一种小型食肉哺乳动物的家养物种10
btw,pivot\u longer
有values\u to
和names\u to
参数,这有助于保存一行重命名。顺便说一句,pivot\u longer
有values\u to
和names\u to
参数,这有助于保存一行重命名。
library(dplyr)
library(tidyr)
labels_tbl %>% pivot_longer(cols = everything()) %>%
select('animal' = name, 'label' = value) %>% inner_join(df)
Joining, by = "animal"
# A tibble: 2 x 3
animal label weight
<chr> <chr> <dbl>
1 dog a domesticated carnivore of the family Canidae 20
2 cat a domestic species of small carnivorous mammal 10