gpt4 book ai didi

r - 左连接两个数据框,没有错误代码但不起作用

转载 作者:行者123 更新时间:2023-12-02 18:35:16 25 4
gpt4 key购买 nike

我有两个 df,我想做 left_join。但由于某种原因我无法将他们聚集在一起。有人可以看一下我哪里做错了吗?没有错误代码,只是来自 cov 的信息都是空的。

df<-structure(list(LBCAT = c("CHEMISTRY", "CHEMISTRY", "CHEMISTRY", 
"CHEMISTRY", "CHEMISTRY", "CHEMISTRY"), LBTEST = c("ALANINE AMINOTRANSFERASE",
"ALANINE AMINOTRANSFERASE", "ALANINE AMINOTRANSFERASE", "ALANINE AMINOTRANSFERASE",
"ALANINE AMINOTRANSFERASE", "ALANINE AMINOTRANSFERASE"), lbstresn = c(6,
11, 26, 21, 14, 13), lbstresu = c("U/L", "U/L", "U/L", "U/L",
"U/L", "U/L"), lbstnrlo = c(6, 6, 6, 6, 6, 6), lbstnrhi = c(37,
37, 37, 37, 37, 37)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))

cov<-structure(list(LBCAT = c("CHEMISTRY", "CHEMISTRY", "CHEMISTRY",
"CHEMISTRY", "CHEMISTRY", "CHEMISTRY", "CHEMISTRY"), LBTESTCD = c("ALT",
"ALT", "ALT", "ALT", "ALT", "ALT", "ALT"), LBTEST = c("Alanine Aminotransferase",
"Alanine Aminotransferase", "Alanine Aminotransferase", "Alanine Aminotransferase",
"Alanine Aminotransferase", "Alanine Aminotransferase", "Alanine Aminotransferase"
), LBORRESU = c("NKAT/L", "IU/L", "MU/ML", "U/L", "UKAT/L", "MCKAT/L",
"IU/ML"), LBSTRESU = c("ukat/L", "ukat/L", "ukat/L", "ukat/L",
"ukat/L", "ukat/L", "ukat/L"), LBSICF = c(0.001, 0.0167, 0.0167,
0.0167, 1, 1, 16.7)), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"))

test <- left_join(df, cov, by = c('LBCAT'= toupper('LBCAT'), 'LBTEST' = toupper('LBTEST'), 'lbstresu' = toupper('LBORRESU')))

enter image description here

最佳答案

我们需要首先mutate将列更改为大写

library(dplyr)
cov %>%
dplyr::mutate(across(c(LBCAT, LBTEST, LBORRESU), toupper)) %>%
left_join(df, ., by = c("LBCAT", "LBTEST", 'lbstresu' = 'LBORRESU'))

-输出

# A tibble: 6 x 9
LBCAT LBTEST lbstresn lbstresu lbstnrlo lbstnrhi LBTESTCD LBSTRESU LBSICF
<chr> <chr> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl>
1 CHEMISTRY ALANINE AMINOTRANSFERASE 6 U/L 6 37 ALT ukat/L 0.0167
2 CHEMISTRY ALANINE AMINOTRANSFERASE 11 U/L 6 37 ALT ukat/L 0.0167
3 CHEMISTRY ALANINE AMINOTRANSFERASE 26 U/L 6 37 ALT ukat/L 0.0167
4 CHEMISTRY ALANINE AMINOTRANSFERASE 21 U/L 6 37 ALT ukat/L 0.0167
5 CHEMISTRY ALANINE AMINOTRANSFERASE 14 U/L 6 37 ALT ukat/L 0.0167
6 CHEMISTRY ALANINE AMINOTRANSFERASE 13 U/L 6 37 ALT ukat/L 0.0167

关于r - 左连接两个数据框,没有错误代码但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68853884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com