【发布时间】:2018-05-16 08:16:14
【问题描述】:
我有一个如下所示的表格:
example <- read.table(text ="
Nr orderedNr Type TextA TextB Year Date
1 469 1 A Text2 Text12 2012 01.01.2015
2 470 8 C Text9 Text19 1961 08.01.2015
3 471 2 A Text3 Text13 2012 02.01.2015
4 472 9 C Text10 Text20 1947 09.01.2015
5 474 3 B Text4 Text14 2005 03.01.2015
6 622 5 A Text6 Text16 1993 05.01.2015
7 623 6 B Text7 Text17 2009 06.01.2015
8 624 7 B Text8 Text18 1964 07.01.2015
9 625 4 C Text5 Text15 2009 04.01.2015
10 626 10 A Text11 Text21 1988 10.01.2015
")
这样我可以粘贴所有行:
rows <- apply(table, 1, paste, collapse=", ")
但它们必须按 orderedNr 排序,我不需要一行的所有列/单元格,而是需要按特殊顺序排列。
一行单元格的打印顺序由行的类型决定。例如:
type A: orderedNr, TextA, TextB, Year, Date
type B: orderedNr, TextB, TextA, Year, Date
type C: orderedNr, Year, TextB, TextA, Date
我的输出应该是这样的:
1, Text2, Text12, 2012, 01.01.2015
2, Text3, Text13, 2012, 02.01.2015
3, Text14, Text4, 2005, 03.01.2015
4, 2009, Text15, Text5, 04.01.2015
...等等。
希望我没有忘记任何事情。
【问题讨论】:
-
“我知道这一行可以做到这一点,但我需要所有行...” 你什么意思?您给出的
apply(...)命令会生成一个向量,其中包含每一行 的串联列条目。此外,table是用户变量的不良名称,因为它掩盖了基本 R 函数table。最后,我完全不清楚您的预期输出应该是什么样子。您可以编辑您的帖子以包含您提供的示例数据的预期输出吗?