sorting based on all columns element and select with a threshold

前端 未结 2 1676
别那么骄傲
别那么骄傲 2021-01-25 06:39

I have a data looks like this

df<- structure(list(V1 = structure(c(6L, 2L, 3L, 7L, 5L, 4L, 8L, 1L
), .Label = c(\"A0A0G2JDV6\", \"P01901\", \"P13745\", \"Q03         


        
相关标签:
2条回答
  • 2021-01-25 07:26

    Edit : The sorting is done wrong in this answer. Please refer to Procrastinatus Maximus's answer where it is correct. Thanks for pointing it out Procrastinatus Maximus :)

    Sort by all the five columns in descending order

    df2 <- df[order(df$V2, df$V3, df$V4, df$V5, df$V6, decreasing = TRUE), ]
    

    Keep only the rows where all values are greater than 1.1

    df3 <- df2[apply(X = df2[paste0("V", 2:6)], MARGIN = 1, FUN = function(x) all(x > 1.1)), ]
    

    Let me know if it helps

    0 讨论(0)
  • 2021-01-25 07:40

    To get the desired output, you can use a combination of order and rowSums. Using:

    df2 <- df[order(rowSums(df[,-1]), decreasing = TRUE),]
    

    gives:

    > df2
              V1        V2        V3        V4        V5        V6
    8 A0A0G2JDV6 2.4361702 2.6861702 2.2872340 2.5797872 2.5585106
    3     P13745 1.5213675 1.5584046 1.7150997 2.0683761 1.7920228
    1     Q3UCW4 1.2345483 1.1854200 1.0681458 1.5356577 1.6053883
    2     P01901 0.9829689 1.1312027 1.0899916 1.2586207 1.2802775
    6     Q03141 1.0357143 0.8972303 0.9278426 1.0466472 1.1836735
    4     Q8CBE6 1.0062305 0.7757009 0.9439252 1.0124611 0.8753894
    5     Q3TMK4 0.8681063 0.7458057 0.7746279 0.8283145 0.8283576
    7     Q8VCQ8 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
    

    To get only the rows in which all the values are higher than 1.1, you can use:

    df2[rowSums(df2[,-1] > 1.1) == 5, ]
    

    which gives:

              V1       V2       V3       V4       V5       V6
    8 A0A0G2JDV6 2.436170 2.686170 2.287234 2.579787 2.558511
    3     P13745 1.521368 1.558405 1.715100 2.068376 1.792023
    
    0 讨论(0)
提交回复
热议问题