# Issue

Suppose I have a dataframe with three columns.

```
a <- c(1,2,3,4)
b <- c(2,4,6,8)
c <- c(3,6,9,12)
df <- cbind(a,b,c)
df
```

This gives you...

```
a b c
[1,] 1 2 3
[2,] 2 4 6
[3,] 3 6 9
[4,] 4 8 12
```

Now suppose I want to create a new dataframe that takes the value TRUE if the value is greater than the column mean and FALSE if it's less than the column mean.

If I use the following command it uses the mean for the whole dataframe.

```
large <- df > mean(df)
large
```

So I get...

```
a b c
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE TRUE
[3,] FALSE TRUE TRUE
[4,] FALSE TRUE TRUE
```

I would like to get

```
a b c
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE FALSE
[3,] TRUE TRUE TRUE
[4,] TRUE TRUE TRUE
```

# Solution

`mean`

gets a single value for the whole `matrix`

, we need `colMeans`

```
df > colMeans(df)[col(df)]
```

Or transpose the dataset, do the comparison and transpose

```
t(t(df) > colMeans(df))
```

Answered By - akrun Answer Checked By - Clifford M. (PHPFixing Volunteer)

## 0 Comments:

## Post a Comment

Note: Only a member of this blog may post a comment.