Silvia
Silvia

Reputation: 405

Extract Specific column with particular value in R

I have a problem. I have a data frame structured like this

 X  a   b   c   d  
 g 0.2 0.4 0.6 0.2  
 h 0.3 0.8  0  0.4 

i want to create a new matrix, with all the rows of my db and with columns with a value major than 0.5, like this

 X   b   c 
 g   /  0.6
 h  0.8  /

Upvotes: 1

Views: 1026

Answers (1)

akrun
akrun

Reputation: 887851

We create a logical matrix (df1[-1] > 0.5), get the colSums, check whether it is greater than 0, subset the dataset based on the logical vector

df2 <- df1[c(TRUE, colSums(df1[-1] > 0.5)>0)]

and assign the values <= 0.5 to NA

df2[-1][df2[-1] <= 0.5] <- NA

Upvotes: 1

Related Questions