【问题标题】:dplyr: add information from one row to another row of that columndplyr:将信息从一行添加到该列的另一行
【发布时间】:2018-01-29 10:59:41
【问题描述】:

我正在尝试将信息从数据框中的一行复制到上面的行。目前,我的数据框如下所示:

                                 word       dur      schwa text

5                                    <NA>   50.0000 FALSE  d
6                            tour de rôle   30.0000  TRUE  @
7                                    <NA>   90.0000 
26                                   <NA>   60.0000 FALSE  d
27                    colonie de vacances   70.0000  TRUE  @
28                                   <NA>   40.0000 FALSE 
                                     <NA>  110.0000 FALSE  d
41                      pantalon de coton   60.0000  TRUE  @
42                                   <NA>   80.0000 FALSE 
43                                   <NA>   90.0000 FALSE 

我想创建一个新的数据框,其中列 word 中的信息(始终在 text=@ 中)始终添加到上面的行中(始终在 text=d 中)。它应该是这样的:

                                   word       dur   schwa text

5                            tour de rôle   50.0000 FALSE  d
6                            tour de rôle   30.0000  TRUE  @
7                                    <NA>   90.0000 
26                    colonie de vacances   60.0000 FALSE  d
27                    colonie de vacances   70.0000  TRUE  @
28                                   <NA>   40.0000 FALSE 
                        pantalon de coton  110.0000 FALSE  d
41                      pantalon de coton   60.0000  TRUE  @
42                                   <NA>   80.0000 FALSE 
43                                   <NA>   90.0000 FALSE 

我将不胜感激每一个建议。我试图使用 mutate-command,但我并没有真正得到任何进一步的结果。

提前致谢!

【问题讨论】:

    标签: r dataframe dplyr


    【解决方案1】:

    将您的输入视为df2

    dput(df2)
    structure(list(id = c(5L, 6L, 7L, 26L, 27L, 28L, 29L, 41L, 42L, 
    43L), word = c(NA, "tour_de_rôle", NA, NA, "colonie_de_vacances", 
    NA, NA, "pantalon_de_coton", NA, NA), dur = c(50, 30, 90, 60, 
    70, 40, 110, 60, 80, 90), schwa = c(FALSE, TRUE, NA, FALSE, TRUE, 
    FALSE, FALSE, TRUE, FALSE, FALSE), text = c("d", "@", NA, "d", 
    "@", NA, "d", "@", NA, NA)), .Names = c("id", "word", "dur", 
    "schwa", "text"), class = "data.frame", row.names = c(NA, -10L
    ))
    

    您可以使用tidyverse 函数并尝试:

    df2 %>%
      mutate( word = if_else(text == 'd' & lead(text,1) == '@', lead(word,1), word))
    

    甚至,根据给定的示例,不使用d

    df2 %>%
      mutate( word = if_else(is.na(word), lead(word,1), word))
    

    给出:

      id                word dur schwa text
    1   5        tour_de_rôle  50 FALSE    d
    2   6        tour_de_rôle  30  TRUE    @
    3   7                <NA>  90    NA <NA>
    4  26 colonie_de_vacances  60 FALSE    d
    5  27 colonie_de_vacances  70  TRUE    @
    6  28                <NA>  40 FALSE <NA>
    7  29   pantalon_de_coton 110 FALSE    d
    8  41   pantalon_de_coton  60  TRUE    @
    9  42                <NA>  80 FALSE <NA>
    10 43                <NA>  90 FALSE <NA>
    

    【讨论】:

    • 非常感谢,mutate-command 工作得很好(事实上它们都是)!
    • @Nadescha 很高兴能为您提供帮助。 :) 您可能还想查看this faq
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2021-07-16
    • 1970-01-01
    • 2019-08-16
    相关资源
    最近更新 更多