【问题标题】:Paste particular cells (ordered) of all rows of a table粘贴表格所有行的特定单元格(有序)
【发布时间】: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。最后,我完全不清楚您的预期输出应该是什么样子。您可以编辑您的帖子以包含您提供的示例数据的预期输出吗?

标签: r string row paste rules


【解决方案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

【讨论】:

  • example &lt;- example[order(example$orderedNr),] 之间可以完美运行!我喜欢lookUP 列表,因为我还不知道我会有多少种类型。之后我可以使用outstring &lt;- paste(example$newColumn) 打印,我的问题就解决了。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-01-10
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2023-01-20
    • 2011-09-08
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    相关资源
    最近更新 更多