粘贴表的所有行的特定单元格(有序)

时间:2018-05-16 08:16:14

标签: r string row paste rules

我有一张看起来像这样的表:

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排序,我不需要一行的所有colums / cells,并且需要特殊的顺序。

应该打印一行单元格的顺序的规则由行的类型决定。例如:

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

......等等。

希望我没有忘记任何事情。

2 个答案:

答案 0 :(得分:1)

制作一个查找列表,使用 apply 循环遍历行,按名称分组,然后使用 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

答案 1 :(得分:0)

这是使用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