Hark
Hark

Reputation: 115

R Select row with lowest numbers and NA's

How I can select the row with the lowest values inside a data frame? I don't search for the lowest value. My interest is for which row all values are lower in comparison to the other rows. Maybe it works without the mean value per row.

Because my data.frame is really huge some ranking would be not so bad, either.

Some example Code:

  v1 <- rnorm(n=10, mean=2, sd=0.55)
  ind <- which(v1 %in% sample(v1, 3))
  v1[ind]<-NA

  v2 <- rnorm(n=10, mean=1, sd=0.3)
  ind <- which(v2 %in% sample(v2, 5))
  v2[ind]<-NA

  v3 <- rnorm(n=10, mean=0.5, sd=0.3)
  ind <- which(v3 %in% sample(v3, 4))
  v3[ind]<-NA

  v4 <- rnorm(n=10, mean=4, sd=1)
  ind <- which(v4 %in% sample(v4, 2))
  v4[ind]<-NA

  v5 <- rnorm(n=10, mean=3, sd=2)
  ind <- which(v5 %in% sample(v5, 1))
  v5[ind]<-NA

  df<-data.frame(v1,v2,v3,v4,v5)

Upvotes: 0

Views: 209

Answers (1)

Randall Helms
Randall Helms

Reputation: 859

It sounds like you want the lowest total value, so add a total column, ignoring the NA values, arrange by total, slice to see your low value.

df$total <- rowSums(df,na.rm = TRUE)

df %>% arrange(total) %>% slice(1)

Note, there's probably a way to do the whole thing in dplyr but I've never really figured out how to make colSums work against a whole tibble/df that you are passing via the pipe. If anyone knows how to do that it would be useful to know!

Upvotes: 1

Related Questions