Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional row removal in an R data frame

Tags:

r

subset

Here is a sample data frame:

df <- data.frame(t1 = c(1,2,3,4), t2 = c(7,3,8,1), t3 = c(1,1,1,1))

df
  t1 t2 t3
1  1  7  1
2  2  3  1
3  3  8  1
4  4  1  1

My goal is to remove the max value from each column. However, for columns like t3, where all values in the column are equal, I would simply remove a single value so that all three columns in the data frame end up with three rows, like below:

df2
  t1 t2 t3
1  1  7  1
2  2  3  1
3  3  1  1
like image 529
bshelt141 Avatar asked May 28 '15 18:05

bshelt141


People also ask

How do I remove rows from multiple conditions in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.

How do I remove a row that contains a specific value in R?

First of all, create a data frame. Then, use single square subsetting with apply function to remove rows that contains a specific number.


2 Answers

How about just using which.max since that would select just one value (i.e. the index of the first occurrence of the maximum value):

as.data.frame(lapply(df, function(x) x[-which.max(x)]))
#   t1 t2 t3
# 1  1  7  1
# 2  2  3  1
# 3  3  1  1
like image 128
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 30 '22 14:10

A5C1D2H2I1M1N2O1R2T1


Try

library(data.table)
 setDT(df)[, lapply(.SD, function(x) x[-which.max(x)])]
like image 35
akrun Avatar answered Oct 30 '22 13:10

akrun