Sort a subset of columns for each row

后端 未结 3 1826
臣服心动
臣服心动 2021-01-16 19:44

I want to sort my.data[4:10] in descending order by row. Some clues here, but I could not parse it sufficiently: Sort second to fifth column for each row in R.

I al

3条回答
  •  -上瘾入骨i
    2021-01-16 20:24

    This seems to work fine:

    my.data[,4:10] <- t(apply(my.data[,4:10], 1,  function(x) sort(x, na.last = T, decreasing=T)))
    
    
    #   habitat NumSites NumSamples Sp1 Sp2 Sp3 Sp4 Sp5 Sp6 Sp7
    #1    Marsh        3          6   3   1  NA  NA  NA  NA  NA
    #2  Prairie        3          5   2   2   2  NA  NA  NA  NA
    #3  Savanna        4          8  67   3   3   1  NA  NA  NA
    #4    Swamp        1          2   2  NA  NA  NA  NA  NA  NA
    #5 Woodland        4          8   2   1   1   1   1  NA  NA
    

提交回复
热议问题