简体   繁体   English

将R中的多行分组

[英]Grouping multiple rows in R

I've generated a heatmap in R for microbiome data, using the following link 我使用以下链接在R中为微生物组数据生成了一个热图

My data as far as rows is concerned looks like this: 就行而言,我的数据如下所示:

781
782
783
547
519
575
044
045
049

If I want to group 781-783, 547-575 and 044-049 as individual groups and give them separate colours using the below idea: 如果我想将781-783、547-575和044-049分组为单独的组,并使用以下思路为它们提供单独的颜色:

Assigning animals to different groups (2 random groups in this case) 将动物分配到不同的组(在这种情况下为2个随机组)

var1 <- round(runif(n=12, min=1, max=2))
var1 <- replace (var1, which(var1 == 1), "deepskyblue")
var1 <- replace (var1, which(var1 == 2), "magenta")
cbind(row.names(data.prop), var1)

How do I go about it? 我该怎么办? I understand that the above code, randomly generates 2 groups, but how can I specify which rows go into which group? 我知道上面的代码随机生成2个组,但是如何指定哪些行进入哪个组?

Thank you, 谢谢,

Susheel Susheel

Because rownames are of necessity character and the only good range-operator in R is ":" for numeric values: you need to coerce ranges to the desired "0nn" format. 因为行名是必需字符,并且R中唯一好的范围运算符是数字值的“:”:您需要将范围强制转换为所需的"0nn"格式。 This is untested in the absence of a proper test case (which questioners are asked to provide): 在没有适当的测试用例的情况下,这是未经测试的(要求提问者提供):

#look at...
sprintf("%03i", c(781:783, 547:575, 044:049))
# then....

 data.prop[ sprintf("%03i", c( 781:783, 547:575, 044:049), 'var1'] <-
      mapply(function(clr, rng) {rep(clr, length(rng) )}, 
                    c("deepskyblue",  "magenta", "green"), 
                    list(  781:783, 547:575, 44:49)
              )

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

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