R:从多个变量的值中定义不同的模式 [英] R: define distinct pattern from values of multiple variables
问题描述
这就是我所拥有的:
data.frame(x=c(0,0,0,1,1,1), y=c(0,0,1,0,1,1))
x y
1 0 0
2 0 0
3 0 1
4 1 0
5 1 1
6 1 1
这里我想要什么:
data.frame(x=c(0,0,0,1,1,1), y=c(0,0,1,0,1,1), pattern=c(1,1,2,3,4,4))
x y pattern
1 0 0 1
2 0 0 1
3 0 1 2
4 1 0 3
5 1 1 4
6 1 1 4
也就是说,我有一堆列(而不仅仅是两列)和数千行。我想遍历每一行,找出x,y,z等的不同组合,将每一个称为不同的模式,然后为每一行返回该模式。
That is, I have a bunch of columns (not just two), and thousands of rows. I want to go through each row, figure out what the distinct combinations of x, y, z, etc. are, call each one a distinct pattern, and return that pattern for each row.
(上下文:我具有多个时间点上多个基因的基因表达数据。我想通过根据在任何时间上调或下调的模式来定义模式,来尝试观察哪些基因随时间而类似地振荡
谢谢。
推荐答案
您可以使用 dplyr :: group_indices()
:
NSE版本
group_indices(df, x, y)
# [1] 1 1 2 3 4 4
SE版本
group_indices_(df, .dots = names(df))
# [1] 1 1 2 3 4 4
不幸的一面此函数的功能在于它不能与 mutate
函数一起使用(还),因此必须将其用作:
The unfortunate side of this function is that it doesn't work with mutate
function (yet), so you have to use it as:
df$pattern <- group_indices(df, x, y)
链接的答案,即使非标准评估版不适用于 mutate
,标准评估版也可以:
From the linked answer, it seems that even though the non-standard evaluation version doesn't work with mutate
, the standard evaluation version does:
df %>% mutate(pattern = group_indices_(df, .dots = c('x', 'y')))
这篇关于R:从多个变量的值中定义不同的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!