Reputation: 55
I have a table which looks like this:
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
")
By this I can paste all rows:
rows <- apply(table, 1, paste, collapse=", ")
But they have to be ordered by orderedNr and I don't need all colums/cells of one row and need them in a special order.
The rules in which order the cells of one row should be printed are determined by the type of row. For example:
type A: orderedNr, TextA, TextB, Year, Date
type B: orderedNr, TextB, TextA, Year, Date
type C: orderedNr, Year, TextB, TextA, Date
My output should look like this:
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
... and so on.
Hopefully I didn't forget anything.
Upvotes: 1
Views: 39
Reputation: 56219
Make a look up list, loop through rows using apply, subset by name, then paste with toString:
# make a lookup list
lookUP <- list(A = c("orderedNr", "TextA", "TextB", "Year", "Date"),
B = c("orderedNr", "TextB", "TextA", "Year", "Date"),
C = c("orderedNr", "Year", "TextB", "TextA", "Date"))
# loop through rows, subset column in certain order, then paste
example$newColumn <-
apply(example, 1, function(i) toString(i[ lookUP[[ i["Type"] ]] ]))
# result
example
# Nr orderedNr Type TextA TextB Year Date newColumn
# 1 469 1 A Text2 Text12 2012 01.01.2015 1, Text2, Text12, 2012, 01.01.2015
# 2 470 8 C Text9 Text19 1961 08.01.2015 8, 1961, Text19, Text9, 08.01.2015
# 3 471 2 A Text3 Text13 2012 02.01.2015 2, Text3, Text13, 2012, 02.01.2015
# 4 472 9 C Text10 Text20 1947 09.01.2015 9, 1947, Text20, Text10, 09.01.2015
# 5 474 3 B Text4 Text14 2005 03.01.2015 3, Text14, Text4, 2005, 03.01.2015
# 6 622 5 A Text6 Text16 1993 05.01.2015 5, Text6, Text16, 1993, 05.01.2015
# 7 623 6 B Text7 Text17 2009 06.01.2015 6, Text17, Text7, 2009, 06.01.2015
# 8 624 7 B Text8 Text18 1964 07.01.2015 7, Text18, Text8, 1964, 07.01.2015
# 9 625 4 C Text5 Text15 2009 04.01.2015 4, 2009, Text15, Text5, 04.01.2015
# 10 626 10 A Text11 Text21 1988 10.01.2015 10, Text11, Text21, 1988, 10.01.2015
Upvotes: 1
Reputation: 378
Here is a possibility using functions from the tidyverse.
require(tidyverse)
example %>%
mutate_if(is.factor, as.character) %>%
mutate(
col1 = orderedNr,
col2 = case_when(
Type == "A" ~ TextA,
Type == "B" ~ TextB,
Type == "C" ~ as.character(Year)),
col3 = case_when(
Type == "A" | Type == "C" ~ TextB,
Type == "B" ~ TextA),
col4 = case_when(
Type == "A" | Type == "B" ~ as.character(Year),
Type == "C" ~ TextA),
col5 = Date) %>%
select(contains("col")) %>%
arrange(col5)
# col1 col2 col3 col4 col5
# 1 1 Text2 Text12 2012 01.01.2015
# 2 2 Text3 Text13 2012 02.01.2015
# 3 3 Text14 Text4 2005 03.01.2015
# 4 4 2009 Text15 Text5 04.01.2015
# 5 5 Text6 Text16 1993 05.01.2015
# 6 6 Text17 Text7 2009 06.01.2015
# 7 7 Text18 Text8 1964 07.01.2015
# 8 8 1961 Text19 Text9 08.01.2015
# 9 9 1947 Text20 Text10 09.01.2015
# 10 10 Text11 Text21 1988 10.01.2015
Upvotes: 0