tidyverse中有R函数可以使用代码选择列中的元素吗?

问题描述 投票:0回答:1

我有两个数据框如下:

数据框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

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

r dplyr tidyverse
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.