多列变异

Multiple column mutate

我有一个包含 100 多列的大数据 table,我想更改 select 中的值。我想 select 按列名排列的列。

df <- data.frame(
  xy_Date = c("2018-12-03","2019-01-02","2019-02-03"),
  ab_Date = c("2018-05-03","2019-10-02","2019-12-03"),
  names = c("Kevin", "Mark", "Jon"))

我想将 xy_Date 和 ab_Date 列从字符类型更改为日期。

library(dplyr)    
df %>% mutate(across(grep("Date", names(df)), ~ as.Date(as.character(.x), "%Y-%m-%d ")))

但是有没有基于 R 的解决方案?

在基础 R 中,您可以使用 lapply -

cols <- grep("Date", names(df))
df[cols] <- lapply(df[cols], as.Date)
df

#     xy_Date    ab_Date names
#1 2018-12-03 2018-05-03 Kevin
#2 2019-01-02 2019-10-02  Mark
#3 2019-02-03 2019-12-03   Jon

str(df)
#'data.frame':  3 obs. of  3 variables:
# $ xy_Date: Date, format: "2018-12-03" "2019-01-02" "2019-02-03"
# $ ab_Date: Date, format: "2018-05-03" "2019-10-02" "2019-12-03"
# $ names  : chr  "Kevin" "Mark" "Jon"
library(dplyr)
df <- data.frame(
  xy_Date = c("2018-12-03","2019-01-02","2019-02-03"),
  ab_Date = c("2018-05-03","2019-10-02","2019-12-03"),
  names = c("Kevin", "Mark", "Jon"))   

new_col_df = df %>% mutate_at(vars(contains('Date')),as.Date)
str(new_col_df)

## Output
# str(new_col_df)
# 'data.frame': 3 obs. of  3 variables:
# $ xy_Date: Date, format: "2018-12-03" "2019-01-02" "2019-02-03"
# $ ab_Date: Date, format: "2018-05-03" "2019-10-02" "2019-12-03"
#  $ names  : chr  "Kevin" "Mark" "Jon"

base R

df[endsWith(names(df), "Date")] <- lapply(df[endsWith(names(df), "Date")], as.Date)

-输出

> str(df)
'data.frame':   3 obs. of  3 variables:
 $ xy_Date: Date, format: "2018-12-03" "2019-01-02" "2019-02-03"
 $ ab_Date: Date, format: "2018-05-03" "2019-10-02" "2019-12-03"
 $ names  : chr  "Kevin" "Mark" "Jon"