age <- c(22,24,NA,27)
duedate <- as.Date(c("1/1/2020",NA,"1/30/2020","12/31/2021"), format="%m/%d/%Y")
df <- data.frame(age,duedate)
Minimum <- sapply(df, function(x) min(x,na.rm=T))
Maximum <- sapply(df, function(x) max(x,na.rm=T))
tbl <- data.frame(min_scale,max_Scale)
print(tbl)
tbl[2,1] <- as.Date(tbl[2,1],origin = "1970-01-01") #Do not work

制作表格后,日期已更改為數值,我無法將其回傳。有什么建議嗎?我想像右表一樣顯示日期。我想盡可能地自動完成,因為我有很多變數。
uj5u.com熱心網友回復:
使用dplyr和的解決方案tidyr。這將適用于多列。
library(dplyr)
library(tidyr)
df2 <- df %>%
summarize(across(.fns = list(Min = min, Max = max), na.rm = TRUE)) %>% # Apply min and max function
mutate(across(.fns = as.character)) %>% # Convert all columns to character
pivot_longer(everything(), names_to = "Parameter") %>% # Convert to long format
separate(Parameter, into = c("Parameter", "Statistics")) %>% # Separate the Parameter columns
pivot_wider(names_from = "Statistics", values_from = "value") # Convert to wide format
df2
# # A tibble: 2 x 3
# Parameter Min Max
# <chr> <chr> <chr>
# 1 age 22 27
# 2 duedate 2020-01-01 2021-12-31
uj5u.com熱心網友回復:
這稍微簡單一點,并導致資料框的方向df與age和duedate在單獨的列中相同:
tbl <- as.data.frame(rbind(Minimum, Maximum))
tbl$duedate <- as.Date(tbl$duedate, "1970-01-01")
tbl
# age duedate
# Minimum 22 2020-01-01
# Maximum 27 2021-12-31
uj5u.com熱心網友回復:
您可以使用基本函式摘要并忽略您不感興趣的統計資料:
summary(df)[c(1,6), ] # for numeric values min and max are the 1st and 6th rows.
#-------------------------
age duedate
Min. :22.00 Min. :2020-01-01
Max. :27.00 Max. :2021-12-31
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348062.html
上一篇:Linux:如何以“%Y/%m/%d/%H/%M”格式將date()舍入到最接近的15分鐘間隔
下一篇:您如何填充缺失的滯后日期?
