snapper
snapper

Reputation: 1197

How to Delete Every Row&Columns Which Contains Negative Value

I have dataframe called lexico which has a dimension of 11293x512.

I'd like to purge every row and column if any element in that column or row holds negative value.

How could I do this?

Following is my code that I tried but it takes too long time to run since it has nested loop structure.

(I was about to first get every column number that holds neg value in it)

colneg <- c()

for(i in 1:11293){
  for(j in 1:512){
    if(as.numeric(as.character(lexico[1283,2]))< 0)
      colneg <- c(colneg, j)
  }
}

It would be appreciate for your harsh advice for this novice.

Upvotes: 0

Views: 492

Answers (1)

Jaap
Jaap

Reputation: 83245

A possible solution:

# create an index of columns with negative values
col_index <- !colSums(d < 0)

# create an index of rows with negative values
row_index <- !rowSums(d < 0)

# subset the dataframe with the two indexes
d2 <- d[row_index, col_index]

What this does:

  • colSums(d < 0) gives a numeric vector of the number of negative values in the columns.
  • By negating it with ! you create a logical vector where for the columns with no negative values get a TRUE value.
  • It works the same for rows.
  • Subsetting the dataframe with the row_index and the col_index gives you a dataframe where the rows as wel as the columns where the negative values appeared are removed.

Reproducible example data:

set.seed(171228)
d <- data.frame(matrix(rnorm(1e4, mean = 3), ncol = 20))

Upvotes: 1

Related Questions