代码之家  ›  专栏  ›  技术社区  ›  claudiadast

如何在R数据帧中查找与字符串关联的两行并减去它们的相互列值

  •  1
  • claudiadast  · 技术社区  · 7 年前

    在R中,我有一个这样的数据帧:

        sample  value  gene  tag       isPTV
    1   1120    3.4    arx1  1120|arx1  0
    2   2123    2.3    mnf2  2123|mnf2  0
    3   1129    1.9    trf4  1129|trf4  0
    4   2198    0.2    brc1  2198|brc1  0
    5   1120    2.1    arx1  1120|arx1  1
    6   2123    0.4    mnf2  2123|mnf2  1
    7   1129    1.2    trf4  1129|trf4  1
    8   2198    0.9    brc1  2198|brc1  1
    

    这样 0 方法 false 1 方法 true . 我最终要做的是为每一个 tag ,查找 value 数字。

    例如,对于 1129|trf4 在两个单独的行中发生。什么时候有价值 isPTV 如果不是,那么绝对值是 1.9 - 1.2 = 0.7 .

    我首先尝试编写一个函数来为给定的 标签 值,这样对于给定的标记,它将返回包含标记的两行:

    getExprValue <- function(dataframe, tag){
      return(dataframe[tag,])
    }
    

    但这不起作用,我不太熟悉如何在R中索引数据帧。

    正确的方法是什么?

    更新:

    解决方案1尝试:

    m_diff <- m %>% group_by(tag) %>% mutate(absDiff = abs(diff(value)))

    回应:

    Error in mutate_impl(.data, dots) : Column 阿斯布迪夫 must be length 1 (the group size), not 0

    解决方案2尝试:

    with(df1, abs(ave(value, tag, FUN = diff)))

    回应:

    Error in x[i] <- value[[j]] : replacement has length zero

    1 回复  |  直到 7 年前
        1
  •  0
  •   prosoitos CodeHunter    7 年前

    编辑:我刚注意到@akrun有一个更简单的解决方案

    使用类似于您的结构创建数据:

    library(tidyverse)
    
    dat <- tibble(
      sample = rep(sample(1000:3000, 10), 2),
      value = rnorm(20, 5, 1),
      gene = rep(letters[1:10], 2),
      tag = paste(sample, gene, sep = "|"),
      isPTV = rep(0:1, each = 10)
    )
    
    dat
    
    #> # A tibble: 20 x 5
    #>    sample value gene  tag    isPTV
    #>     <int> <dbl> <chr> <chr>  <int>
    #>  1   2149  5.90 a     2149|a     0
    #>  2   1027  5.46 b     1027|b     0
    #>  3   1103  5.65 c     1103|c     0
    #>  4   1884  4.86 d     1884|d     0
    #>  5   2773  5.58 e     2773|e     0
    #>  6   2948  6.98 f     2948|f     0
    #>  7   2478  5.17 g     2478|g     0
    #>  8   2724  6.71 h     2724|h     0
    #>  9   1927  5.06 i     1927|i     0
    #> 10   1081  4.39 j     1081|j     0
    #> 11   2149  4.60 a     2149|a     1
    #> 12   1027  2.97 b     1027|b     1
    #> 13   1103  6.17 c     1103|c     1
    #> 14   1884  5.83 d     1884|d     1
    #> 15   2773  4.23 e     2773|e     1
    #> 16   2948  6.48 f     2948|f     1
    #> 17   2478  5.06 g     2478|g     1
    #> 18   2724  5.32 h     2724|h     1
    #> 19   1927  7.32 i     1927|i     1
    #> 20   1081  4.73 j     1081|j     1
    

    @Akrun解决方案(比我的好很多):

    dat %>%
      group_by(tag) %>%
      mutate(absDiff = abs(diff(value)))
    
    #> # A tibble: 20 x 6
    #> # Groups:   tag [10]
    #>    sample value gene  tag    isPTV absDiff
    #>     <int> <dbl> <chr> <chr>  <int>   <dbl>
    #>  1   2149  5.90 a     2149|a     0   1.30 
    #>  2   1027  5.46 b     1027|b     0   2.49 
    #>  3   1103  5.65 c     1103|c     0   0.520
    #>  4   1884  4.86 d     1884|d     0   0.974
    #>  5   2773  5.58 e     2773|e     0   1.34 
    #>  6   2948  6.98 f     2948|f     0   0.502
    #>  7   2478  5.17 g     2478|g     0   0.114
    #>  8   2724  6.71 h     2724|h     0   1.39 
    #>  9   1927  5.06 i     1927|i     0   2.26 
    #> 10   1081  4.39 j     1081|j     0   0.337
    #> 11   2149  4.60 a     2149|a     1   1.30 
    #> 12   1027  2.97 b     1027|b     1   2.49 
    #> 13   1103  6.17 c     1103|c     1   0.520
    #> 14   1884  5.83 d     1884|d     1   0.974
    #> 15   2773  4.23 e     2773|e     1   1.34 
    #> 16   2948  6.48 f     2948|f     1   0.502
    #> 17   2478  5.06 g     2478|g     1   0.114
    #> 18   2724  5.32 h     2724|h     1   1.39 
    #> 19   1927  7.32 i     1927|i     1   2.26 
    #> 20   1081  4.73 j     1081|j     1   0.337
    

    我的初步建议(不必要的复杂):

    nested <- dat %>%
      group_by(tag) %>%
      nest()
    
    nested %>%
      mutate(difference = map(data, ~ abs(diff(.$value)))) %>%
      select(- data) %>% 
      unnest()
    
    #> # A tibble: 10 x 2
    #>    tag    difference
    #>    <chr>       <dbl>
    #>  1 2149|a      1.30 
    #>  2 1027|b      2.49 
    #>  3 1103|c      0.520
    #>  4 1884|d      0.974
    #>  5 2773|e      1.34 
    #>  6 2948|f      0.502
    #>  7 2478|g      0.114
    #>  8 2724|h      1.39 
    #>  9 1927|i      2.26 
    #> 10 1081|j      0.337