FAQ
Hi,

I would like to select the most frequent value level in a set of three variables.

Three different observators have judged hair color in study subjects. Mostly they judge the same color, sometimes there is a slight difference. I want to know what most of the observators have chosen (so at least 2) from the 3 observations. E.g. If two out of three observators decide the hair is black, then it's likely not to be brown.

Let's say that i have 3 variables: color1, color2, color3. Each have 4 possible levels (fair up to black, 1-4). I would like a new variable containing this 'most frequent judgement'.

I have already searched through the knowledge base and many posts but I haven't found what I'm looking for.

Is this possible?

Sam

## Search Discussions

•  at Aug 20, 2012 at 11:47 am ⇧
I found a solution to this problem myself by searching further and experimenting with some functions.

I ended up with a for-loop which iterates over the cases. It generates a table per case, and selects the most frequent (i.e. the value corresponding with the highest frequency) choice, which checked to be non-NA and saved to the new variable. I still haven't found a way to re-factor them using the same attributes as the original color-variables... Anyone an idea?

for(i in seq(length(ss\$name))) {
color <- names(which.max(table(c(ss\$color1[i], ss\$color2[i], ss\$color3[i]))))

if(is.null(color))
{
ss\$color_med[i] <- NA
}
else
{
ss\$color_med[i] <- as.integer(color)
}
}

Greetings!
Sam

Op 20-aug-2012, om 10:48 heeft Sam Dekeyser het volgende geschreven:
Hi,

I would like to select the most frequent value level in a set of three variables.

Three different observators have judged hair color in study subjects. Mostly they judge the same color, sometimes there is a slight difference. I want to know what most of the observators have chosen (so at least 2) from the 3 observations. E.g. If two out of three observators decide the hair is black, then it's likely not to be brown.

Let's say that i have 3 variables: color1, color2, color3. Each have 4 possible levels (fair up to black, 1-4). I would like a new variable containing this 'most frequent judgement'.

I have already searched through the knowledge base and many posts but I haven't found what I'm looking for.

Is this possible?

Sam

•  at Aug 20, 2012 at 2:08 pm ⇧
Hi

It is really a typical example of a question which has probably very simple solution but hardly anybody can give you a rasonable answer.

What is the structure of your data?

set.seed(1)
x<-sample(1:4, 60, replace=T)
mat<-as.factor(x)
dim(mat) <- c(20,3)
sapply(apply(mat,1, table), max)
[1] 2 1 2 1 1 2 2 2 2 1 2 1 2 2 2 1 1 1 2 2
names(sapply(apply(mat,1, table), which.max))
[1] "4" "1" "3" "1" "1" "4" "1" "2" "3" "1" "2" "1" "2" "1" "4" "1" "2" "1" "3"
[20] "2"

gives you the most frequent value in each row of matrix mat.

Petr

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Sam Dekeyser
Sent: Monday, August 20, 2012 10:48 AM
To: r-help at r-project.org
Subject: [R] select most frequent value in set of variables

Hi,

I would like to select the most frequent value level in a set of three
variables.

Three different observators have judged hair color in study subjects.
Mostly they judge the same color, sometimes there is a slight
difference. I want to know what most of the observators have chosen (so
at least 2) from the 3 observations. E.g. If two out of three
observators decide the hair is black, then it's likely not to be brown.

Let's say that i have 3 variables: color1, color2, color3. Each have 4
possible levels (fair up to black, 1-4). I would like a new variable
containing this 'most frequent judgement'.

I have already searched through the knowledge base and many posts but I
haven't found what I'm looking for.

Is this possible?

Sam

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
guide.html
and provide commented, minimal, self-contained, reproducible code.
•  at Aug 20, 2012 at 4:04 pm ⇧
HI,

Slightly different way:
unlist(lapply(apply(mat,1,count),function(x) max(x[2])))
?#[1] 2 1 2 1 1 2 2 2 2 1 2 1 2 2 2 1 1 1 2 2

----- Original Message -----
From: PIKAL Petr <petr.pikal@precheza.cz>
To: Sam Dekeyser <sam.dekeyser@gmail.com>; "r-help at r-project.org" <r-help@r-project.org>
Cc:
Sent: Monday, August 20, 2012 10:08 AM
Subject: Re: [R] select most frequent value in set of variables

Hi

It is really a typical example of a question which has probably very simple solution but hardly anybody can give you a rasonable answer.

What is the structure of your data?

set.seed(1)
x<-sample(1:4, 60, replace=T)
mat<-as.factor(x)
dim(mat) <- c(20,3)
sapply(apply(mat,1, table), max)
[1] 2 1 2 1 1 2 2 2 2 1 2 1 2 2 2 1 1 1 2 2
names(sapply(apply(mat,1, table), which.max))
[1] "4" "1" "3" "1" "1" "4" "1" "2" "3" "1" "2" "1" "2" "1" "4" "1" "2" "1" "3"
[20] "2"

gives you the most frequent value in each row of matrix mat.

Petr

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Sam Dekeyser
Sent: Monday, August 20, 2012 10:48 AM
To: r-help at r-project.org
Subject: [R] select most frequent value in set of variables

Hi,

I would like to select the most frequent value level in a set of three
variables.

Three different observators have judged hair color in study subjects.
Mostly they judge the same color, sometimes there is a slight
difference. I want to know what most of the observators have chosen (so
at least 2) from the 3 observations. E.g. If two out of three
observators decide the hair is black, then it's likely not to be brown.

Let's say that i have 3 variables: color1, color2, color3. Each have 4
possible levels (fair up to black, 1-4). I would like a new variable
containing this 'most frequent judgement'.

I have already searched through the knowledge base and many posts but I
haven't found what I'm looking for.

Is this possible?

Sam

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.
•  at Aug 24, 2012 at 10:03 am ⇧
Hi,

Shortly after my first post I posted an answer including the fix I found; which seems to work. Through the archives I found that my code snippet got filtered out and appended as an attachment (which was not my intent).

This was my suggestion:

for(i in seq(length(ss\$name))) {
color <- names(which.max(table(c(ss\$color1[i], ss\$color2[i], ss\$color3[i]))))

if(is.null(color))
{
ss\$color_med[i] <- NA
}
else
{
ss\$color_med[i] <- as.integer(color)
}
}

Sam

Op 20-aug-2012, om 18:04 heeft arun het volgende geschreven:
HI,

Slightly different way:
unlist(lapply(apply(mat,1,count),function(x) max(x[2])))
#[1] 2 1 2 1 1 2 2 2 2 1 2 1 2 2 2 1 1 1 2 2

----- Original Message -----
From: PIKAL Petr <petr.pikal@precheza.cz>
To: Sam Dekeyser <sam.dekeyser@gmail.com>; "r-help at r-project.org" <r-help@r-project.org>
Cc:
Sent: Monday, August 20, 2012 10:08 AM
Subject: Re: [R] select most frequent value in set of variables

Hi

It is really a typical example of a question which has probably very simple solution but hardly anybody can give you a rasonable answer.

What is the structure of your data?

set.seed(1)
x<-sample(1:4, 60, replace=T)
mat<-as.factor(x)
dim(mat) <- c(20,3)
sapply(apply(mat,1, table), max)
[1] 2 1 2 1 1 2 2 2 2 1 2 1 2 2 2 1 1 1 2 2
names(sapply(apply(mat,1, table), which.max))
[1] "4" "1" "3" "1" "1" "4" "1" "2" "3" "1" "2" "1" "2" "1" "4" "1" "2" "1" "3"
[20] "2"

gives you the most frequent value in each row of matrix mat.

Petr

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Sam Dekeyser
Sent: Monday, August 20, 2012 10:48 AM
To: r-help at r-project.org
Subject: [R] select most frequent value in set of variables

Hi,

I would like to select the most frequent value level in a set of three
variables.

Three different observators have judged hair color in study subjects.
Mostly they judge the same color, sometimes there is a slight
difference. I want to know what most of the observators have chosen (so
at least 2) from the 3 observations. E.g. If two out of three
observators decide the hair is black, then it's likely not to be brown.

Let's say that i have 3 variables: color1, color2, color3. Each have 4
possible levels (fair up to black, 1-4). I would like a new variable
containing this 'most frequent judgement'.

I have already searched through the knowledge base and many posts but I
haven't found what I'm looking for.

Is this possible?

Sam

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.
•  at Aug 22, 2012 at 8:33 am ⇧

On 08/20/2012 06:48 PM, Sam Dekeyser wrote:
Hi,

I would like to select the most frequent value level in a set of three variables.

Three different observators have judged hair color in study subjects. Mostly they judge the same color, sometimes there is a slight difference. I want to know what most of the observators have chosen (so at least 2) from the 3 observations. E.g. If two out of three observators decide the hair is black, then it's likely not to be brown.

Let's say that i have 3 variables: color1, color2, color3. Each have 4 possible levels (fair up to black, 1-4). I would like a new variable containing this 'most frequent judgement'.

I have already searched through the knowledge base and many posts but I haven't found what I'm looking for.

Is this possible?
Hi Sam,
Are you looking for the Mode function in the prettyR package?

Jim

## Related Discussions

Discussion Overview
 group r-help categories r posted Aug 20, '12 at 8:48a active Aug 24, '12 at 10:03a posts 6 users 4 website r-project.org irc #r

### 4 users in discussion

Content

People

Support

Translate

site design / logo © 2022 Grokbase