【发布时间】:2019-04-10 11:36:49
【问题描述】:
我有一个庞大的病理结果数据集。每个患者都有一个唯一的标识符(在这种情况下为row_id。对于每个患者,他们在特定日期采集了样本(sample_date)。他们进行的测试范围非常多样化,并且输出不一(一些带有字符串和一些数字)。此外,并不是每个患者都在每个sample_date 进行过所有测试,因此应该有不少 NA。
执行的测试名称在test_name 列中,结果在result 列中。
我想把它变成一个广泛的数据集,使用test_name 作为列标题传播result 列,但将标识符保持为row_id 和sample_date。
tidyr 中的新 pivot_wider() 函数似乎非常适合我的需求,当我运行它时,它为我提供了我需要的数据框类型(即,行仍然由 row_id 和 sample_date 标识,但现在每个 test_name 和其中的结果都有列。
这是我的数据集的一个小样本:
structure(list(row_id = 1:81, sample_date = structure(c(16444,
16444, 16444, 16444, 16444, 16444, 16444, 16444, 16444, 16444,
16444, 16444, 16444, 16444, 16444, 16444, 16444, 16444, 16444,
16444, 16444, 16444, 16444, 16444, 16444, 16444, 16444, 16444,
16444, 16447, 16447, 16447, 16447, 16447, 16447, 16447, 16447,
16447, 16447, 16447, 16447, 16447, 16447, 16447, 16447, 16447,
16447, 16447, 16447, 16447, 16447, 16447, 16447, 16447, 16448,
16448, 16448, 16448, 16448, 16448, 16442, 16442, 16442, 16442,
16442, 16442, 16442, 16442, 16442, 16442, 16442, 16442, 16442,
16442, 16442, 16442, 16442, 16442, 16442, 16442, 16442), class = "Date"),
test_name = c("Epidemic Typhus Group IgG Abs", "Epidemic Typhus Group IgM Abs",
"Spotted Fever Group IgG Abs", "Spotted Fever Group IgM Abs",
"Albumin", "Alkaline phosphatase", "Alanine transaminase",
"Basophils", "Bilirubin (total)", "Creatinine", "C-reactive protein",
"Eosinophils", "Estimated GFR", "Haemoglobin (g/L)", "HCT",
"Potassium", "Lymphocytes", "MCHC (g/L)", "MCH", "MCV", "Monocytes",
"MPV", "Sodium", "Neutrophils", "Platelet count", "Red cell count",
"RDW", "Urea", "White cell count", "Albumin", "Alkaline phosphatase",
"Alanine transaminase", "Basophils", "Bilirubin (total)",
"Creatinine", "C-reactive protein", "Eosinophils", "Estimated GFR",
"Haemoglobin (g/L)", "HCT", "Potassium", "Lymphocytes", "MCHC (g/L)",
"MCH", "MCV", "Monocytes", "MPV", "Sodium", "Neutrophils",
"Platelet count", "Red cell count", "RDW", "Urea", "White cell count",
"Creatinine", "C-reactive protein", "Estimated GFR", "Potassium",
"Sodium", "Urea", "Albumin", "Alkaline phosphatase", "Alanine transaminase",
"APTT Ratio", "APTT", "Basophils", "Bilirubin (total)", "Creatinine",
"C-reactive protein", "Eosinophils", "Fibrinogen", "Estimated GFR",
"Haemoglobin (g/L)", "HCT", "INR", "Potassium", "Lymphocytes",
"MCHC (g/L)", "MCH", "MCV", "Monocytes"), result = c("Not detected",
"Not detected", "Not detected", "Not detected", "47", "84",
"29", "0.3% 0.03", "12", "98", "3.3", "1.7% 0.15", "77\r\nUnits: mL/min/1.73sqm\r\nMultiply eGFR by 1.21 for people of African\r\nCaribbean origin. Interpret with regard to UK CKD\r\nguidelines: www.renal.org/information-resources\r\nUse with caution for adjusting drug dosages -\r\ncontact clinical pharmacist for advice.",
"156", "0.435", "3.8", "25.7% 2.31", "359", "30.4", "84.6",
"7.1% 0.64", "10.1", "140", "65.2% 5.86", "240", "5.14",
"12.4", "3.9", "8.99", "45", "53", "41", "0.3% 0.03", "10",
"59", "2.0", "2.8% 0.32", ">90\r\nUnits: mL/min/1.73sqm\r\nMultiply eGFR by 1.21 for people of African\r\nCaribbean origin. Interpret with regard to UK CKD\r\nguidelines: www.renal.org/information-resources\r\nUse with caution for adjusting drug dosages -\r\ncontact clinical pharmacist for advice.",
"126", "0.398", "4.5", "28.7% 3.30", "317", "25.7", "81.2",
"5.7% 0.65", "10.8", "143", "62.5% 7.18", "411", "4.90",
"14.7", "3.5", "11.49", "59", "76.2", ">90\r\nUnits: mL/min/1.73sqm\r\nMultiply eGFR by 1.21 for people of African\r\nCaribbean origin. Interpret with regard to UK CKD\r\nguidelines: www.renal.org/information-resources\r\nUse with caution for adjusting drug dosages -\r\ncontact clinical pharmacist for advice.",
"4.2", "139", "3.4", "46", "47", "40", "1.3", "39", "0.4% 0.01",
"8", "74", "7.0", "0.4% 0.01", "2.50", ">90\r\nUnits: mL/min/1.73sqm\r\nMultiply eGFR by 1.21 for people of African\r\nCaribbean origin. Interpret with regard to UK CKD\r\nguidelines: www.renal.org/information-resources\r\nUse with caution for adjusting drug dosages -\r\ncontact clinical pharmacist for advice.",
"146", "0.441", "0.96", "4.3", "43.2% 1.14", "331", "29.1",
"87.8", "6.8% 0.18")), class = "data.frame", row.names = c(NA,
-81L))
这是我使用的pivot_wider() 代码(上面调用了path_results 的数据集:
library(tidyr)
path_results_wide <- path_results %>%
select(row_id, sample_date, test_name, result)%>%
pivot_wider(
id_cols = c(row_id,
sample_date),
names_from = test_name,
values_from = result
)
有些列应该是数字,有些应该是字符串,但pivot_wider() 已将它们全部解析为字符列表,当我尝试将它们更改为数字时,出现以下错误:
path_results_wide$Albumin <- as.numeric(path_results_wide$Albumin)
错误:无法将
> 转换为
非常欢迎任何关于我可以做些什么来解决这个问题的建议。 谢谢。
【问题讨论】: