简体   繁体   中英

R min and max function does not work for dates

I have a dataframe,df, with a date variable.

   locationID organe    
    <int>    <date>    
      1    1940-04-01
      2    1938-07-01
      3    1938-07-01
      4    1938-07-01

I want to compare the dates with a fixed time point,like "1938-12-1", to find the earliest date. I used the min function but failed.

df %>% mutate(earliest=min(organe,as.Date("1938-12-1")))


locationID organe     earliest  
<int>      <date>     <date>    
1       1940-04-01 1937-09-01
2       1938-07-01 1937-09-01
3       1938-07-01 1937-09-01
4       1938-07-01 1937-09-01

I don't know why min function does not work here, though it works well for the following situation

min(as.Date("1938-07-01"),as.Date("1938-12-1"))
[1] "1938-07-01"

Could anybody help?

We can use pmin/pmax to get the minimum/maximum when one or both of the input argument is a vector of length greater than 1 (if both have length greater than 1, assumes the length to be same)

df %>% 
  mutate(earliest=pmin(organe,as.Date("1938-12-1")))
# A tibble: 4 x 3
#  locationID organe     earliest  
#      <int> <date>     <date>    
#1          1 1940-04-01 1938-12-01
#2          2 1938-07-01 1938-07-01
#3          3 1938-07-01 1938-07-01
#4          4 1938-07-01 1938-07-01

Or apply min after rowwise

df %>% 
   rowwise %>% 
   mutate(earliest=min(organe, as.Date("1938-12-1")))

Note that min returns a single value as output ie

min(5:1, 3)
#[1] 1

min(5:3, 1)
#[1] 1

For a vectorized minimum, use pmin . According to ?min

pmax*() and pmin*() take one or more vectors as arguments, recycle them to common length and return a single vector giving the 'parallel' maxima (or minima) of the argument vectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM