首页 > 解决方案 > tidyverse 中是否有 R 函数可以使用代码选择列的元素?

问题描述

我有两个数据框如下:

数据框 1

df1 <- read.table(text = " Place	Code	Name
A	12	Hogo
                  C	14	Smith
                  C	17	Rose
                  D	16	John
                  A	19	Noor
                  B	12	Hogo
                  C	16	John
                  D	19	Noor
                  A	24	Matt
                  D	23	Kim
                  ", header = TRUE)

数据框 2

df2 <- read.table(text = " Code Name
Code	Name
12	Hogo
14	Smith
17	Rose
16	John
19	Noor
24	Matt
", header = TRUE)

我想根据以下代码选择A和C

Place 	Code	Name
A	12	Hogo
C	14	Smith
C	17	Rose
C	16	John
A	19	Noor
A	24	Matt

我已经搜索过,但没有找到解决方案。非常感谢您的帮助。

标签: rdplyrtidyverse

解决方案


我认为错误正在发生,因为在 DF2 中你有两次列名(如 Artem Sokolov 所示),并且 R 正在读取代码作为因子而不是整数值。

 df2 <- read.table(text = " Code Name
    12  Hogo
    14  Smith
    17  Rose
    16  John
    19  Noor
    24  Matt
    ", header = TRUE)

然后运行连接,你应该没问题。


推荐阅读