【发布时间】:2019-04-04 23:08:43
【问题描述】:
R 新手并试图充分利用它。这是一项每周需要多次完成的任务。
在工作中,我们得到一个包含以下数据的 SAS 代码文本文件:
1,2,3,201,202,203 = "Screening"
101,102,301,302,404,405,1001= "Cycle 1 Day 1"
1002 = "Cycle 1 Day 2"
1003 = "Cycle 1 Day 3"
103,104,303,304,407,408,409,410 = "Cycle 1 Day 8"
105,106,305,306,412,413 = "Cycle 1 Day 15"
107,108,307,308,414,415,416,417,1022= "Cycle 1 Day 22"
1023 = "Cycle 1 Day 23"
1024 = "Cycle 1 Day 24"
109,110,309,310,418,419,420,421,2001 = "Cycle 2 Day 1"
2002= "Cycle 2 Day 2"
2003= "Cycle 2 Day 3"
111,112,422,423 = "Cycle 2 Day 8"
113,114,311,312,424,425 = "Cycle 2 Day 15"
115,116,426,427= "Cycle 2 Day 22"
117,118,119,313,314,315 = "Cycle 2 End of Cycle"
120,121,316,317,430,431 = "Cycle 3 Day 1"
122,123,432,433 = "Cycle 3 Day 8"
124,125,318,319,434,435 = "Cycle 3 Day 15"
126,127,436,437 = "Cycle 3 Day 22"
128,129,320,321,438,439 = "Cycle 4 Day 1"
我还有一个 Excel 文件,内容如下:
Visit No Vis Label
1 Screening Day -14 to -1
2 Screening Day -14 to -1
3 Screening Day -14 to -1
101 Cycle 1 Day 1 to 3
102 Cycle 1 Day 1 to 3
103 Cycle 1 Day 8
104 Cycle 1 Day 8
105 Cycle 1 Day 15
106 Cycle 1 Day 15
107 Cycle 1 Day 22
108 Cycle 1 Day 22
109 Cycle 2 Day 1
110 Cycle 2 Day 1
111 Cycle 2 Day 8
112 Cycle 2 Day 8
113 Cycle 2 Day 15
114 Cycle 2 Day 15
115 Cycle 2 Day 22
116 Cycle 2 Day 22
117 Cycle 2 End of Cycle
118 Cycle 2 End of Cycle
119 Cycle 2 End of Cycle
现在我必须将 SAS 代码 txt 文件与 Excel 文件进行比较,并记下 SAS 文件中缺少哪些文件。
我尝试读取 SAS txt 文件,该文件充满了空白,并且能够将其删除并通过以下方式获取内容:
d <- read.delim("testSAS.txt", sep = ":", strip.white = TRUE, skip = 2, header = FALSE)
结果如下:
V1 V2
1 1,2,3,201,202,203 Screening
2 101,102,301,302,404,405,1001 Cycle 1 Day 1
3 1002 Cycle 1 Day 2
4 1003 Cycle 1 Day 3
5 103,104,303,304,407,408,409,410 Cycle 1 Day 8
6 105,106,305,306,412,413 Cycle 1 Day 15
现在我想将 V1 值分开,这样每个值都会创建一个新行,并附加相同的值,如下所示:
1 Screening
2 Screening
3 Screening
201 Screening
202 Screening
203 Screening
我使用以下代码使其更具延展性,但它似乎返回了一个变形的列表:
df<- data.frame(matrix(unlist(d), nrow = 61, byrow = T),stringsAsFactors = FALSE)
现在想将它与 Excel 文件进行比较,以便最终生成一个 txt 或 xlsx,说明哪些 V1 值不存在。
有比操作 SAS txt 文件更快的方法吗?我应该如何处理这个?从 excel 到 txt 是否更快,反之亦然?
欢迎任何建议!
【问题讨论】:
-
您应该reshape SAS 数据,然后执行
anti-join。这将从x返回y中不存在的行。请参阅?join了解如何操作。使用openxlsx::read.xlsx阅读您的xlsx表。 -
如果您需要更多帮助,请务必发帖reproducible example。
-
好问题。您应该查看
haven(以“整洁”的方式将 SAS 数据导入 R)和readxl(对 Excel 也是如此)。然后查找tidyverse。一旦你拥有了它,我认为你将拥有你需要的一切(为此以及更多)。祝你好运 -
您的 SAS 代码看起来像是 SAS 格式定义的一部分。您可以访问 SAS 吗?您可以只运行代码来定义格式,然后将定义转换为数据集。
-
@Tom,我目前还无法访问 SAS,可能在一个月后。这是同事用来对 excel 文件执行质量检查的脚本。您是说在 R 中运行 SAS 格式 def?