编辑使其更清洁.假设我有两个数据表(dt1和dt2),我想使用数据表获取dt3.A,B,C,E,F,G,H是列名.dt1键是列A,dt2键是列E.数据表具有不同的行数.我想保留DT1中的所有列,并且只将一个列(H)从DT2添加到已连接的数据表中.最终,我将它存储为DT1(虽然我在下面显示为dt3).
如何使用数据表实现它?我有一个丑陋的解决方案与合并+数据框架.
dt1
A B C
1 4 7
2 5 8
3 6 9
2 20 21
dt2
E F G H
1 10 13 16
3 12 15 18
2 11 14 17
dt3
A B C H
1 4 7 16
2 5 8 17
3 6 9 18
2 20 21 17
Run Code Online (Sandbox Code Playgroud) install.packages("data.table")
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/data.table_1.12.8.tgz'
Content type 'application/x-gzip' length 2117137 bytes (2.0 MB)
downloaded 2.0 MB
The downloaded binary packages are in
/var/folders/r1/1rsn2y0j78v907qgv0btm_fm0000gn/T//RtmppBu3UK/downloaded_packages
Run Code Online (Sandbox Code Playgroud)
使用提供的代码更新到最新的开发版本:
data.table::update.dev.pkg()
Run Code Online (Sandbox Code Playgroud)
打印的控制台:
Warning: unable to access index for repository https://Rdatatable.gitlab.io/data.table/bin/macosx/el-capitan/contrib/3.5:
cannot open URL 'https://Rdatatable.gitlab.io/data.table/bin/macosx/el-capitan/contrib/3.5/PACKAGES'
Package which is only available in source form, and may need compilation of C/C++/Fortran: ‘data.table’
Do you want to attempt to install these from sources? (Yes/no/cancel) Yes
installing the source package ‘data.table’
trying URL 'https://Rdatatable.gitlab.io/data.table/src/contrib/data.table_1.12.9.tar.gz'
Content type 'application/gzip' length 5189945 bytes (4.9 MB) …Run Code Online (Sandbox Code Playgroud) 我有以下数据框架
df = data.frame(Code=seq(80,105,1))
Run Code Online (Sandbox Code Playgroud)
我需要添加另一个tCode从Code列计算的列.Code具有广泛的价值观.对于每个给定的范围,我需要具有特定值tCode.我无法使用cut函数来执行此任务.范围和预期结果给我.我只能想到这个冗长的ifelse结构:
df$tCode = ifelse(df$Code > 102, 81,
ifelse(df$Code %in% seq(101,102,1),80,
ifelse(df$Code %in% seq(99,100,1),79,
ifelse(df$Code %in% seq(97,89,1),78,
ifelse(df$Code %in% seq(95,96,1),77,
ifelse(df$Code %in% seq(92,94,1),76,
ifelse(df$Code %in% seq(90,91,1),75,
ifelse(df$Code %in% seq(88,89,1),74,
ifelse(df$Code %in% seq(86,87,1),73,
ifelse(df$Code %in% seq(84,85,1),72,
ifelse(df$Code %in% seq(82,83,1),71,
ifelse(df$Code %in% seq(80,81,1),70,1))))))))))))
Run Code Online (Sandbox Code Playgroud)
我觉得这不是解决这个问题的最佳方法.还有更好的建议吗?