r - 使用 vlookup 值在数据框中添加新列
问题描述
我在 R 中有一个数据框。
Col1 Col2
1 21
1 20
我想要下面的输出。
Col1 Col2 Output
1 21 21
1 20 21
本质上,我想在 Col1 上为 Col2 应用查找并将结果存储在输出列中。这将确保对于第 1 列中的相同输入,将在查找中选择列中两个值中较高的值。
我已经尝试了几种方法来做到这一点,但似乎都没有奏效。
更新的数据框 -
Col1 Col2 Output
1 21 21
1 20 21
12 20 20
12 19 20
解决方案
我们可以通过 'Col1' 进行分组并更新max
'Col2' 的值
library(dplyr)
df1 %>%
group_by(Col1) %>%
mutate(Ouput = max(Col2)) %>%
ungroup
推荐阅读
- bootstrap-4 - Webpack Encore 在单独导出中构建主题?
- database - SQL *Loader 并发程序中如何上传数字类型数据?
- react-native - 如何在本机反应中根据用户选择使用多语言键盘
- r - 传入变量名时,循环遍历 group_by
- amazon-web-services - 有多少百分比的 Amazon DocumentDB RAM 用于索引,多少百分比用于数据?
- passwords - 将用户名和密码传递给 sqlcmd,
- arduino - 是否可以将带有 Ethernet Shield 的 Arduino 连接到带有本地 Blynk 服务器的 Raspberry Pi 并使用该服务器?
- python - 使用没有交叉验证的 LTSM 进行时间序列预测
- sql-server - 'dotnet' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
- linux - 如何制作二进制文件,使这个文件的解释器是/lib/ld-linux-aarch64.so?