[英]R - Function that selects specific character in a column in data frame and replaces it
[英]Append a character to a specific location in a column in a data frame
我有这样的数据框
df <- data.frame(
'Week' = c(27,28,29),
'date' = c("2019-W (01-Jul)","2019-W (08-Jul)","2019-W (15-Jul)"))
我需要在日期列中的W之后的 append 周列
expecteddf <- data.frame(
'Week' = c(27,28,29),
'date' = c("2019-W27 (01-Jul)","2019-W28 (08-Jul)","2019-W29 (15-Jul)"))
如何在 R 中实现这一点?
提前致谢!!
您可以将paste0
与sub
组合使用,即
paste0(sub(' .*', '', df$date), df$Week, sub('.* ', ' ', df$date))
#[1] "2019-W27 (01-Jul)" "2019-W28 (08-Jul)" "2019-W29 (15-Jul)"
在基础 R 中,您还可以使用regmatches
+ regexpr
检查解决方案@Darren以详细说明模式(?<=W)
regmatches(df$date, regexpr("(?<=W)", df$date, perl = TRUE)) <- df$Week
df
Week date
1 27 2019-W27 (01-Jul)
2 28 2019-W28 (08-Jul)
3 29 2019-W29 (15-Jul)
使用stringr::str_replace
,可以将替换向量化:
library(stringr)
df$date = str_replace(df$date, "W", paste0("W", df$Week))
df
# Week date
# 1 27 2019-W27 (01-Jul)
# 2 28 2019-W28 (08-Jul)
# 3 29 2019-W29 (15-Jul)
或者,我们可以采用日期格式化方法。 将您的date
列转换为实际的Date
class ( df$Date
,如下),然后我们可以将实际的Date
转换为您想要的格式(或任何其他格式)。
df$Date = as.Date(df$date, format = "%Y-W (%d-%b)")
df$result = format(df$Date, format = "%Y-W%V (%d-%b)")
df
# Week date Date result
# 1 27 2019-W (01-Jul) 2019-07-01 2019-W27 (01-Jul)
# 2 28 2019-W (08-Jul) 2019-07-08 2019-W28 (08-Jul)
# 3 29 2019-W (15-Jul) 2019-07-15 2019-W29 (15-Jul)
具有sub(..., perl = T)
的base
解决方案:
within(df, date <- Vectorize(sub)("(?<=W)", Week, date, perl = T))
笔记:
"(?<=W)"
匹配"W"
后面的 position 。sub()
的前两个arguments不能向量化,所以这里需要Vectorize()
或者mapply()
。 相应的str_replace()
版本,它是矢量化的。
library(dplyr)
library(stringr)
df %>%
mutate(date = str_replace(date, "(?<=W)", as.character(Week)))
Output
# Week date
# 1 27 2019-W27 (01-Jul)
# 2 28 2019-W28 (08-Jul)
# 3 29 2019-W29 (15-Jul)
基本 R 选项使用:
gsub
+ Vectorize
expecteddf <- within(df,date <- Vectorize(gsub)("W",paste0("W",Week),date))
gsub
+ mapply
expecteddf <- within(
df,
date <- mapply(function(x, p) gsub("(.*W)(\\s.*)", sprintf("\\1%s\\2", p), x), date, Week)
)
您可以在 str_c 中使用 mutate
library(tidyverse)
df %>%
mutate(date = str_c(str_sub(date,1,6),
Week,
str_sub(date,7)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.