识别R中六个变量的所有组合 [英] Identify all combinations of six variables in R

查看:60
本文介绍了识别R中六个变量的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含6个变量和250个观察值的数据框,如下所示:

I have a data frame with 6 variables and 250 observations that looks as follows:

   id    Var1    Var2    Var3    Var4    Var5    Var6 **

   1     yes     yes     yes     no      yes     yes
   2     no      no      yes     yes     no      yes
   ...
   250   no      yes     yes     yes     yes     yes

我想确定数据中存在的变量的所有组合.例如,我知道每个变量有20个观察结果,结果为是".

I want to identify all combinations of variables present in the data. For example, I know there are 20 observations with "yes" for each variable.

我正在做一个对等分组分析,并希望根据这些是/否变量对观察结果进行分组.每个变量带有"yes"的20个观测值将是第1组,其他20个观测值的Var1 =是,而Var2:Var6 = no将是第2组,依此类推...

I am doing a peer grouping analysis and want to group the observations based on these yes/no variables. The 20 observations with "yes" to each variable will be group#1, 20 other observations have Var1=yes and Var2:Var6=no will be group#2, etc...

我尝试在plyr中使用count,如下所示:

I attempted to use count in plyr as follows:

> count(dataframe[,-1])

这不起作用.任何建议都会很棒!

This did not work. Any suggestions will be great!

推荐答案

您可以使用 interaction paste(...,sep ="_")进行组合,但是然后您需要对它们进行一些操作.将它们拆分到单独的类别中(这将保留身份),或使用 table 将它们制成表格(或同时用两者).

You can either use interaction or paste( ..., sep="_") to make the combinations, but then you need to do something with them. Either split them into separate categories (which will preserve identities) or tabulate them with table (or both).

 int_grps <- split( dataframe[,1], interaction( dataframe[,-1], drop=TRUE) )

 int_counts <- table( interaction( dataframe[,-1], drop=TRUE ) )

如果您只想枚举存在的组合,则代码可能是:

If you only wanted to enumerate the combinations that exist, the code could be:

names(table(interaction( dataframe[,-1], drop=TRUE)) )    

这篇关于识别R中六个变量的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆