PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

[FIXED] How to apply different conditions to each column of a dataframe?

 November 22, 2022     multiple-columns, multiple-conditions, r     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing