从R中不同表中的另一个列值获取一个表中的列值

时间:2016-10-17 11:37:24

标签: r

以下是R

中的两个示例表

表1

ID  Name
1   a
2   b
3   c
4   a
5   b
6   c

表2

ID  Age
2   20
4   40
6   60
10  100
50  500

我想创建一个表格,如下所示(在Age中添加一列Table1,并根据ID列从Table2中提取值:

输出表

ID  Name  Age
1   a      -
2   b     20
3   c      -
4   a     40
5   b      -
6   c     60

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:-1)

你可以使用sqldf库。

?sqldf

SQL select on data frames

Description

SQL select on data frames


# import the sqldf library. 
# if you get "Error in library(sqldf) : there is no package called 'sqldf' you can install it with install.packages('sqldf')
library(sqldf)

# your first df
table1 <- read.csv('/tmp/df1.csv',stringsAsFactors = F)

table2 <- read.csv('/tmp/df2.csv',stringsAsFactors = F)

table2

# express your query in terms of an SQL statement.
# in this case you want ALL the records from the first table and ALL the records from the second one which are 'matching' with the records in the first one
# In terms of SQL, this is a LEFT JOIN statement
sql <- 'select table1.id, name, age from table1 left join table2 on (table1.id = table2.id)'

# run your query
sqldf(sql)

这就是结果:

id name age
 1  1    a   -
 2  2    b  20
 3  3    c   -
 4  4    a  40
 5  5    b   -
 6  6    c  60

注意:如果你没有table2中id为1,3,5的匹配记录,你可以将sql语句从左连接更改为LEFT OUTER JOIN。请随时向我询问进一步的解释,谢谢。