-2

I am trying to find the minimum value in each row in a database that looks like this

V1    V2   V3    V4     V5  
3      2    4    10     11

and I have tried two different methods to get the minimum value

apply(temp[,],1,min)

and

apply(temp, 1, FUN = min)

I know these are essentially the same thing. Instead of getting a result of 2, I get a result of 10. If I remove the 10, the result become 11. I would only like a result of 2 and am unsure of how to obtain this.

Cali567
  • 1
  • 1
  • 2
    `apply(temp, 1, min)` works for me. Please add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) using `dput()` so that it is easy for others to help you. – Ronak Shah Dec 29 '17 at 03:27
  • 1
    This is not reproducible: when I run it with my scraped data, it gives me 2. It sounds like it's doing a string-comparison, suggesting you are looking at `factor`s, in which case either (a) your assumption of it being numerical data is incorrect, (b) there are other columns you are not showing that are causing the automatic conversion to `character`, or (c) you *know* that it is not `numeric` and are mistaken on what string-comparisons should return here. – r2evans Dec 29 '17 at 03:31
  • (BTW: if `temp` has any `character` columns, then `apply(temp,...)` will convert everything to `character`. To avoid that, you need to subset your `data.frame` inside the apply, ala `apply(temp[,2:6], 1, min)`.) – r2evans Dec 29 '17 at 03:35

3 Answers3

3

I suggest that your data is actually character (or possibly factor), and not numeric. The following code should give the results you want:

df <- data.frame(V1=c("3"), V2=c("2"), V3=c("4"), V4=c("10"), V5=c("11"))
apply(df, 1, FUN = function(x) { min(as.numeric(x)) })

The reason 10 is coming up as the minimum is because it is the minimum value, at least lexicographically. By converting that data to a numeric type, we get the expected results.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • This is what my comment was steering towards ... AND my belief is that there is something else fundamentally wrong here (data assumptions, coding mistakes, ...), in which case this is just a band-aid on top of several other problems. (Good answer, btw, just wonder what else is happening to cause this.) – r2evans Dec 29 '17 at 03:32
  • @r2evans Agreed. Whenever something like this pops up in an R question (or a database question) there is usually a smell there. – Tim Biegeleisen Dec 29 '17 at 03:33
1

We can do pmin

do.call(pmin, c(df, list(na.rm = TRUE)))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

I have read the comments above stating that this is not reproducible, and the answer above pointing out the issue with data types. I have made sure that the data frame consists of numeric values which are not factors. And indeed the result is 2. Factor data types can mess with the output.

df <- data.frame(V1=c(3), V2=c(2), V3=c(4), V4=c(10), V5=c(11));
apply(df,1,min)