简体   繁体   English

R熔化的数据帧等级

[英]R melted dataframe rank

I Have a data frame that looks something like this: 我有一个看起来像这样的数据框:

> head(female.meth.ordered)
        Var1                                     Var2      value RankMeth
1 cg25296477 ES__WA09_passage39_Female____87.1429.1.1 0.85581970        1
2 cg01003813 ES__WA09_passage39_Female____87.1429.1.1 0.91677790        1
3 cg13176022 ES__WA09_passage39_Female____87.1429.1.1 0.04714496        1
4 cg26484667 ES__WA09_passage39_Female____87.1429.1.1 0.85785770        1
5 cg21028156 ES__WA09_passage39_Female____87.1429.1.1 0.04065772        1
6 cg11503671 ES__WA09_passage39_Female____87.1429.1.1 0.82933710        1

There are 606528 rows to this data frame. 此数据帧有606528行。 Row Var2 contains 54 unique sample names. Var2行包含54个唯一的样本名称。

> unique(female.meth.ordered$Var2)

[1] ES__WA09_passage39_Female____87.1429.1.1                   
 [2] ES__WA09_passage39_Female____87.1429.2.1                   
 [3] ES__MEL4_passage35_Female____127.378.3.1                   
 [4] ES__CSC14_passage29_Female____197.1296.1.2                 
 [5] ES__CM6_passage19_Female____244.622.1.1                    
 [6] ES__HES2_passage105_Female____32.135.4.1  
54 Levels: ES.parthenote__LLC15_passage45_Female____317.905.1.1 ...

I want to assign the "RankMeth" column a rank of 1 for the first 10 unique hits in "Var2" column. 我想为“ Var2”列中的前10个唯一匹配分配“ RankMeth”列的等级为1。 Then assign "RankMeth" column a rank of 2 for the next 10 unique hits in the "Var2" column. 然后将“ Var2”列中的下10个唯一匹配项分配给“ RankMeth”列2级。 And so on for ranks 3,4,5. 以此类推,排名3、4、5。

The easiest solution could be as: 最简单的解决方案可能是:

Approach: 做法:

Take the unique Var2 and rank by dividing row_number with 10 . 采用唯一的Var2并通过将row_number除以10排名。 This will provide Var2 with rank in group of 1-10 . 这将为Var2提供1-10组的排名。 Say its the meth_rank . 说出meth_rank

Join meth_rank with 'female.meth.ordered to find out corresponding MethRank` for rows. meth_rank与“ female.meth.ordered”连接起来, to find out corresponding MethRank`行。

meth_rank <- unique(female.meth.ordered$Var2) %>% as.data.frame() %>% 
     mutate(RankMeth = ceiling(row_number()/10))

colnames(meth_rank) <- c("Var2", "RankMeth")
#Join meth_rank with female.meth.ordered to populate rank.
female.meth.ordered %>% 
  select(-RankMeth) %>%
  inner_join(meth_rank, by="Var2") 
#Result will be generated with headings as
# Var1        Var2      value RankMeth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM