R 如何逐行识别数据帧中的未知模式?

R 如何逐行识别数据帧中的未知模式?,r,dataframe,design-patterns,row,analysis,R,Dataframe,Design Patterns,Row,Analysis,我有一个数据框架,其中我有连续15年的农业使用代码(1-5)。每行是一个多边形,表示一个字段。最终,我需要R来遍历行,识别使用模式,并告诉我它们各自的频率。不幸的是,在我的真实数据集中,我有超过100万。特征和所有可能的模式都是未知的 a <- data.frame(replicate(15, sample(0:5,500,rep=TRUE))) colnames(a) <- paste0("use",2005:2019) id <- c(1:500) a <- cbin

我有一个数据框架,其中我有连续15年的农业使用代码(1-5)。每行是一个多边形,表示一个字段。最终,我需要R来遍历行,识别使用模式,并告诉我它们各自的频率。不幸的是,在我的真实数据集中,我有超过100万。特征和所有可能的模式都是未知的

a <- data.frame(replicate(15, sample(0:5,500,rep=TRUE)))
colnames(a) <- paste0("use",2005:2019)
id <- c(1:500)
a <- cbind(id,a)

id use2005 use2006 use2007 use2008 use2009 use2010 use2011 use2012 use2013 use2014 use2015 ...
1  1       1       1       1       1       2       2       1       4       4       4       ...
2  4       4       4       4       5       5       5       0       5       5       5       ...
3  1       4       3       2       3       2       4       5       1       1       1       ...
4  1       1       1       1       1       2       2       1       4       4       4       ...
5  4       2       2       2       2       5       3       3       3       3       3       ...

a这里有一个示例,介绍如何使用一个小示例数据集(即您发布的数据集)实现这一点

库(tidyverse)
#示例数据集
a=读取。表格(文本=”
id使用2005使用2006使用2007使用2008使用2009使用2010使用2011使用2012使用2013使用2014使用2015
1  1       1       1       1       1       2       2       1       4       4       4      
2  4       4       4       4       5       5       5       0       5       5       5      
3  1       4       3       2       3       2       4       5       1       1       1      
4  1       1       1       1       1       2       2       1       4       4       4      
5  4       2       2       2       2       5       3       3       3       3       3
“,页眉=T)
a%>%
每行的组嵌套(id)%>%#
mutate(pattern=map(data,~paste(.x,collapse=“,”))%%>%#将模式创建为字符串
unnest(模式)%>%#unnest模式列
计数(模式,排序=T)#计数模式
##tibble:4 x 2
#图案n
#                    
# 1 1,1,1,1,1,2,2,1,4,4,4     2
# 2 1,4,3,2,3,2,4,5,1,1,1     1
# 3 4,2,2,2,2,5,3,3,3,3,3     1
# 4 4,4,4,4,5,5,5,0,5,5,5     1      

以下是一个如何使用小示例数据集(即您发布的数据集)实现此目的的示例

库(tidyverse)
#示例数据集
a=读取。表格(文本=”
id使用2005使用2006使用2007使用2008使用2009使用2010使用2011使用2012使用2013使用2014使用2015
1  1       1       1       1       1       2       2       1       4       4       4      
2  4       4       4       4       5       5       5       0       5       5       5      
3  1       4       3       2       3       2       4       5       1       1       1      
4  1       1       1       1       1       2       2       1       4       4       4      
5  4       2       2       2       2       5       3       3       3       3       3
“,页眉=T)
a%>%
每行的组嵌套(id)%>%#
mutate(pattern=map(data,~paste(.x,collapse=“,”))%%>%#将模式创建为字符串
unnest(模式)%>%#unnest模式列
计数(模式,排序=T)#计数模式
##tibble:4 x 2
#图案n
#                    
# 1 1,1,1,1,1,2,2,1,4,4,4     2
# 2 1,4,3,2,3,2,4,5,1,1,1     1
# 3 4,2,2,2,2,5,3,3,3,3,3     1
# 4 4,4,4,4,5,5,5,0,5,5,5     1      
也许是这个

library(tidyverse)
a[, -1] %>% group_by_all %>% count
#  use2005 use2006 use2007 use2008 use2009 use2010 use2011 use2012 use2013 use2014 use2015     n
#     <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int> <int>
# 1       1       1       1       1       1       2       2       1       4       4       4     2
# 2       1       4       3       2       3       2       4       5       1       1       1     1
# 3       4       2       2       2       2       5       3       3       3       3       3     1
# 4       4       4       4       4       5       5       5       0       5       5       5     1
也许是这个

library(tidyverse)
a[, -1] %>% group_by_all %>% count
#  use2005 use2006 use2007 use2008 use2009 use2010 use2011 use2012 use2013 use2014 use2015     n
#     <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int> <int>
# 1       1       1       1       1       1       2       2       1       4       4       4     2
# 2       1       4       3       2       3       2       4       5       1       1       1     1
# 3       4       2       2       2       2       5       3       3       3       3       3     1
# 4       4       4       4       4       5       5       5       0       5       5       5     1

这太棒了,也谢谢你!有没有办法保留字段的ID,以便我以后知道哪些字段具有哪种模式?也许会有一个额外的列列出ID?是的,您可以创建一个包含元素的列(a)ID向量,(b)ID作为一个由逗号分隔的字符串(如模式列)。您能知道如何做到这一点吗?或者你想让我更新我的答案?这很好,也谢谢你!有没有办法保留字段的ID,以便我以后知道哪些字段具有哪种模式?也许会有一个额外的列列出ID?是的,您可以创建一个包含元素的列(a)ID向量,(b)ID作为一个由逗号分隔的字符串(如模式列)。您能知道如何做到这一点吗?或者你想让我更新我的答案吗?谢谢,这很有帮助!!有没有办法保留字段的ID?所以我以后知道哪些字段有哪些模式?谢谢,这非常有用!!有没有办法保留字段的ID?所以我后来知道哪些字段有哪些模式?
a %>%
  group_by_at(vars(-id)) %>%
  summarise(n = n(), ids = paste(id, collapse= "," ))
#   use2005 use2006 use2007 use2008 use2009 use2010 use2011 use2012 use2013 use2014 use2015     n ids  
#     <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int>   <int> <int> <chr>
# 1       1       1       1       1       1       2       2       1       4       4       4     2 1,4  
# 2       1       4       3       2       3       2       4       5       1       1       1     1 3    
# 3       4       2       2       2       2       5       3       3       3       3       3     1 5    
# 4       4       4       4       4       5       5       5       0       5       5       5     1 2