如何在R中的数据行内跨变量/向量查找模式 [英] How to find mode across variables/vectors within a data row in R

查看:84
本文介绍了如何在R中的数据行内跨变量/向量查找模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道如何找到模式(R中单个案例的所有变量中最常见的模式?

Does anyone know how to find the mode (most frequent across variables for a single case in R?

例如,如果我有关于喜欢的水果类型(x)的数据,则对调查中的每个受访者(id)进行九次询问(x1-x9).如果我想在询问的前五次中找到每个测试对象的模态响应,我将如何在R中进行编程?

For example, if I had data on favorite type of fruit (x), asked nine times (x1-x9) for each respondent (id) in a survey. If I wanted to find the modal response for each test subject in the first five times asked, how would I program that in R?

更简洁地说,下面的示例数据中,如何找到每种情况下的MODE?

More succinctly, with the example data is below, how do I find the MODE within each case?

 id  x1  x2  x3  x4  x5  MODE(x1-x5)?  
  1  3   5   6   4   5   5   
  2  7   4   7   4   7   7  
  3  3   4   4   4   3   4  
  4  3   2   2   2   3   2 

推荐答案

modeest 包提供了用于实现单峰单变量数据模式的多个估计器.

The modeest package provides implements a number of estimators of the mode for unimodal univariate data.

此函数具有返回最频繁值的函数mfv,或者(如?mfv所述)最好使用`mlv(...,method ='discrete')

This has a function mfv to return the most frequent value, or (as ?mfv states) it is perhaps better to use `mlv(..., method = 'discrete')

library(modeest)


## assuming your data is in the data.frame dd

apply(dd[,2:6], 1,mfv)
[1] 5 7 4 2
## or
apply(dd[,2:6], 1,mlv, method = 'discrete')
[[1]]
Mode (most frequent value): 5 
Bickel's modal skewness: -0.2 
Call: mlv.integer(x = newX[, i], method = "discrete") 

[[2]]
Mode (most frequent value): 7 
Bickel's modal skewness: -0.4 
Call: mlv.integer(x = newX[, i], method = "discrete") 

[[3]]
Mode (most frequent value): 4 
Bickel's modal skewness: -0.4 
Call: mlv.integer(x = newX[, i], method = "discrete") 

[[4]]
Mode (most frequent value): 2 
Bickel's modal skewness: 0.4 
Call: mlv.integer(x = newX[, i], method = "discrete") 

现在,如果您的关系最频繁,那么您需要考虑一下自己想要的.
mfvmlv.integer都将返回所有最常见的值. (尽管打印方法只显示一个值)

Now, if you have ties for the most frequent, then you need to think about what you want.
both mfv and mlv.integer will return all the values that tie for the most frequent. (although the print method only shows a single value)

这篇关于如何在R中的数据行内跨变量/向量查找模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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