r - 如何将值从一个表复制到另一个表
问题描述
我有两个分组在一起的数据框(rbind 或 SQL 中的 Union)。数据可以更好地表示,因为现在用户看到的是两行而不是一行。具体来说,用户希望在一行而不是两行中查看酒店价格和其他酒店信息(两行是因为必须创建 rbind-dummy 列以匹配列数)。以下是要实现的示例(请注意,两个数据帧中的属性不匹配):
df1 <- data.frame(Property = paste0("Property", "_", letters[1:30]),Dates = seq.Date(as.Date("2018-08-01"), as.Date("2018-08-10"), "d"), RateAmount = abs(rnorm(10) * 200),
Occupancy = NA)
df2 <- data.frame(Property = paste0("Property", "_", letters[1:10]),Dates = seq.Date(as.Date("2018-08-01"), as.Date("2018-08-10"), "d"), RateAmount = NA,
Occupancy = rnorm(rnorm(10) * 200, mean = 85))
rbind(df1, df2)
第一个表的全部目的是收集酒店价格 (RateAmounts),而第二个表(最后 10 行)包含除 RateAmounts 之外的所有内容。我想将 RateAmount 信息(非 NA)转移到 df2 并基本上摆脱 df1 (以单行而不是每个“属性”两行结束)。我尝试了不同的连接(在日期上使用 dplyr)无济于事。先感谢您
解决方案
您正在尝试执行相当于连接的操作。Rmerge
可以很好地做到这一点。
首先预处理数据以去除数据框中的 NA 列:
df1_new <- df1[,-4]
df2_new <- df2[,-3]
下一个合并:
merge(df1_new, df2_new)
Merge 有参数来控制要合并的列,以及在不匹配的情况下要保留哪些数据行。
或者在一行中:merge(df1[,-4],df2[,-3])
这仅保留匹配的值:
Property Dates RateAmount Occupancy
1 Property_a 2018-08-01 77.0288921 84.97796
2 Property_b 2018-08-02 234.1155289 84.67847
3 Property_c 2018-08-03 347.3747234 86.52871
4 Property_d 2018-08-04 0.7615909 85.14532
5 Property_e 2018-08-05 71.6080894 83.76476
6 Property_f 2018-08-06 171.5747186 86.05425
7 Property_g 2018-08-07 67.7377617 83.22948
8 Property_h 2018-08-08 23.6833664 86.16361
9 Property_i 2018-08-09 468.6021860 82.85337
10 Property_j 2018-08-10 250.3918442 85.03492
如果您想保留所有值,例如可以设置 all = TRUE
merge(df1[,-4],df2[,-3],all=TRUE)
Property Dates RateAmount Occupancy
1 Property_a 2018-08-01 77.0288921 84.97796
2 Property_b 2018-08-02 234.1155289 84.67847
3 Property_c 2018-08-03 347.3747234 86.52871
4 Property_d 2018-08-04 0.7615909 85.14532
5 Property_e 2018-08-05 71.6080894 83.76476
6 Property_f 2018-08-06 171.5747186 86.05425
7 Property_g 2018-08-07 67.7377617 83.22948
8 Property_h 2018-08-08 23.6833664 86.16361
9 Property_i 2018-08-09 468.6021860 82.85337
10 Property_j 2018-08-10 250.3918442 85.03492
11 Property_k 2018-08-01 77.0288921 NA
12 Property_l 2018-08-02 234.1155289 NA
13 Property_m 2018-08-03 347.3747234 NA
14 Property_n 2018-08-04 0.7615909 NA
15 Property_NA 2018-08-07 67.7377617 NA
16 Property_NA 2018-08-08 23.6833664 NA
17 Property_NA 2018-08-09 468.6021860 NA
18 Property_NA 2018-08-10 250.3918442 NA
19 Property_o 2018-08-05 71.6080894 NA
20 Property_p 2018-08-06 171.5747186 NA
21 Property_q 2018-08-07 67.7377617 NA
22 Property_r 2018-08-08 23.6833664 NA
23 Property_s 2018-08-09 468.6021860 NA
24 Property_t 2018-08-10 250.3918442 NA
25 Property_u 2018-08-01 77.0288921 NA
26 Property_v 2018-08-02 234.1155289 NA
27 Property_w 2018-08-03 347.3747234 NA
28 Property_x 2018-08-04 0.7615909 NA
29 Property_y 2018-08-05 71.6080894 NA
30 Property_z 2018-08-06 171.5747186 NA
推荐阅读
- android - 在 Android 上通过以太网 eth0 接口 Ping 和其他人
- java - 只打印java中文本第一行上的字符串
- ios - 使用 EXC_CORPSE_NOTIFY 启动 iOS 应用程序崩溃
- spring-boot - spring-kafka:在 Spring Boot 中使用交互式查询
- python-3.x - Python3 类型系统:调用参数中的特定属性?
- parsing - 如何使时间解析谓词双向工作?
- javascript - “此语言功能仅支持 ECMASCRIPT6 或更高模式:箭头功能。”
- r - 快速帮助,scale_color_manual 不显示多行图例
- google-apps-script - 从自定义函数中的范围获取值返回“#ERROR!” 在加载期间
- css - CSS 选择器在这段代码中是如何工作的?“类 + element.class”