找到多个日期列中的最小和最大日期,使用R。

huangapple go评论99阅读模式
英文:

Find Min and Max date from multiple date columns using R

问题

  1. # 创建新的列 maxdate 和 mindate
  2. df$maxdate <- apply(df[1:4], 1, max, na.rm = TRUE)
  3. df$mindate <- apply(df[1:4], 1, min, na.rm = TRUE)
英文:

I would like to create a new column called maxdate and mindate from a list of date columns assuming 4 date columns and has missing values.

The solution below only gives the max/min of the rows of the columns. I am interested in finding max/min date across the columns.

  1. df$maxdate &lt;- apply (df[1:4], 1, max, na.rm = TRUE)
  1. df &lt;- data.frame(
  2. col1 = c(&quot;11/09/1999&quot;, &quot;11/09/1999&quot;, &quot;11/09/1999&quot;, &quot;11/09/1999&quot;, &quot;11/09/1999&quot;),
  3. col2 = c(&quot;01/01/2000&quot;, &quot;01/01/2000&quot;, &quot;01/01/2000&quot;, &quot;01/01/2000&quot;, &quot;01/01/2000&quot;),
  4. col3 = c(&quot;12/09/1961&quot;, &quot;10/03/1995&quot;, &quot;30/03/1992&quot;, &quot;25/05/1992&quot;, &quot;25/05/1992&quot;),
  5. col4 = c(&quot;01/01/1930&quot;, &quot;01/01/1939&quot;, &quot;01/01/1942&quot;, &quot;01/01/1936&quot;, &quot;01/01/1937&quot;)
  6. )

sample data

  1. col1 col2 col3 col4
  2. 11/09/1999 01/01/2000 12/09/1961 01/01/1930
  3. 11/09/1999 01/01/2000 10/03/1995 01/01/1939
  4. 11/09/1999 01/01/2000 30/03/1992 01/01/1942
  5. 11/09/1999 01/01/2000 25/05/1992 01/01/1936
  6. 11/09/1999 01/01/2000 25/05/1992 01/01/1937

答案1

得分: 0

  1. 库(dplyr)
  2. df = 数据框(date1 = c("2023-05-11", "2023-04-12","2023-07-13","2023-01-14","2023-05-15"),
  3. date2 = c("2023-04-11", "2023-07-12","2023-09-13","2023-05-14","2023-12-15"),
  4. date3 = c("2023-08-11", "2023-06-12","2023-08-13","2023-08-14","2023-05-15"),
  5. date4 = c("2023-01-11", "2023-05-12","2023-05-13","2023-12-14","2023-05-15"))
  6. df <- df %>% mutate_all(as.Date)
  7. # 编辑:删除了rowwise并添加了na.rm=TRUE,因为您似乎希望从所有行中获取最大值,忽略NAs?
  8. df <- df %>% mutate(max_date = max(date1, date2,date3,date4, na.rm=TRUE))
  9. df
英文:
  1. library(dplyr)
  2. df = data.frame(date1 = c(&quot;2023-05-11&quot;, &quot;2023-04-12&quot;,&quot;2023-07-13&quot;,&quot;2023-01-14&quot;,&quot;2023-05-15&quot;),
  3. date2 = c(&quot;2023-04-11&quot;, &quot;2023-07-12&quot;,&quot;2023-09-13&quot;,&quot;2023-05-14&quot;,&quot;2023-12-15&quot;),
  4. date3 = c(&quot;2023-08-11&quot;, &quot;2023-06-12&quot;,&quot;2023-08-13&quot;,&quot;2023-08-14&quot;,&quot;2023-05-15&quot;),
  5. date4 = c(&quot;2023-01-11&quot;, &quot;2023-05-12&quot;,&quot;2023-05-13&quot;,&quot;2023-12-14&quot;,&quot;2023-05-15&quot;))
  6. df &lt;- df %&gt;% mutate_all(as.Date)
  7. # edit: removed rowwise and added na.rm=TRUE, as you seem to want the max from all rows, disregarding NAs?
  8. df &lt;- df %&gt;% mutate(max_date = max(date1, date2,date3,date4, na.rm=TRUE))
  9. df

答案2

得分: 0

翻译的部分如下:

  1. df <- data.frame(
  2. col1 = c("11/09/1999", "11/09/1999", "11/09/1999", "11/09/1999", "11/09/1999"),
  3. col2 = c("01/01/2000", "01/01/2000", "01/01/2000", "01/01/2000", "01/01/2000"),
  4. col3 = c("12/09/1961", "10/03/1995", "30/03/1992", "25/05/1992", "25/05/1992"),
  5. col4 = c("01/01/1930", "01/01/1939", "01/01/1942", "01/01/1936", "01/01/1937")
  6. )
  7. maxdate <- lapply(df, function (x) max(x, na.rm = TRUE))
  8. > maxdate
  9. $col1
  10. [1] "11/09/1999"
  11. $col2
  12. [1] "01/01/2000"
  13. $col3
  14. [1] "30/03/1992"
  15. $col4
  16. [1] "01/01/1942"

请注意,由于行数与列数不相等,因此我未将其分配给列,因此您不能将其添加到数据框中。

英文:

Like this?

  1. df &lt;- data.frame(
  2. col1 = c(&quot;11/09/1999&quot;, &quot;11/09/1999&quot;, &quot;11/09/1999&quot;, &quot;11/09/1999&quot;, &quot;11/09/1999&quot;),
  3. col2 = c(&quot;01/01/2000&quot;, &quot;01/01/2000&quot;, &quot;01/01/2000&quot;, &quot;01/01/2000&quot;, &quot;01/01/2000&quot;),
  4. col3 = c(&quot;12/09/1961&quot;, &quot;10/03/1995&quot;, &quot;30/03/1992&quot;, &quot;25/05/1992&quot;, &quot;25/05/1992&quot;),
  5. col4 = c(&quot;01/01/1930&quot;, &quot;01/01/1939&quot;, &quot;01/01/1942&quot;, &quot;01/01/1936&quot;, &quot;01/01/1937&quot;)
  6. )
  7. maxdate &lt;- lapply(df, function (x) max(x, na.rm = TRUE))
  8. &gt; maxdate
  9. $col1
  10. [1] &quot;11/09/1999&quot;
  11. $col2
  12. [1] &quot;01/01/2000&quot;
  13. $col3
  14. [1] &quot;30/03/1992&quot;
  15. $col4
  16. [1] &quot;01/01/1942&quot;

Note that I did not assign it to a column as the number of rows != the number of columns so you cannot add it to your dataframe.

huangapple
  • 本文由 发表于 2023年5月17日 17:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定