按ID替换行

时间:2018-04-16 06:16:01

标签: r

我有两个数据框full

library(dplyr)

full %>% glimpse()
Observations: 2,919
Variables: 10
$ Id           <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15...
$ BsmtQual     <fct> Gd, Gd, Gd, TA, Gd, Gd, Ex, Gd, TA, TA, TA, Ex, T...
$ BsmtCond     <fct> TA, TA, TA, Gd, TA, TA, TA, TA, TA, TA, TA, TA, T...
$ BsmtExposure <fct> No, Gd, Mn, No, Av, No, Av, Mn, No, No, No, No, N...
$ BsmtFinType1 <fct> GLQ, ALQ, GLQ, ALQ, GLQ, GLQ, GLQ, ALQ, Unf, GLQ,...
$ BsmtFinSF1   <int> 706, 978, 486, 216, 655, 732, 1369, 859, 0, 851, ...
$ BsmtFinType2 <fct> Unf, Unf, Unf, Unf, Unf, Unf, Unf, BLQ, Unf, Unf,...
$ BsmtFinSF2   <int> 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, ...
$ BsmtUnfSF    <int> 150, 284, 434, 540, 490, 64, 317, 216, 952, 140, ...
$ TotalBsmtSF  <int> 856, 1262, 920, 756, 1145, 796, 1686, 1107, 952, ...

这一个dat1

dat1 %>% glimpse()
Observations: 88
Variables: 10
$ Id           <int> 18, 40, 91, 103, 157, 183, 260, 333, 343, 363, 37...
$ BsmtQual     <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
$ BsmtCond     <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
$ BsmtExposure <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
$ BsmtFinType1 <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
$ BsmtFinSF1   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
$ BsmtFinType2 <fct> No, No, No, No, No, No, No, No, No, No, No, No, N...
$ BsmtFinSF2   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
$ BsmtUnfSF    <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
$ TotalBsmtSF  <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...

我希望通过匹配Id将full中的行替换为dat1行。我使用mergetidyverse进行了尝试但是没有用。

3 个答案:

答案 0 :(得分:1)

选项是使用jsonReceivedEnvValues = json::parse(strJson); int count = jsonReceivedEnvValues["Data"].count("Bla"); anti_join的组合,然后使用semi_join进行合并。以下解决方案将提供所需的记录。

bind_rows

答案 1 :(得分:0)

在R中使用SQLDF库,并在full和data1之间的id上进行内连接。从完整数据框中选择变量。

答案 2 :(得分:-1)

您可以使用rbindcbind(如果没有正在运行的示例,很难说出来):

## Combining by columns (what seems to be your case)
combined <- cbind(full, dat1)
## Combining by rows
combined <- rbind(full, dat1)