简体   繁体   English

Append 一个字符到数据帧的列中的特定位置

[英]Append a character to a specific location in a column in a data frame

I am having a data frame like this我有这样的数据框

df <- data.frame(
       'Week' = c(27,28,29),
       'date' = c("2019-W (01-Jul)","2019-W (08-Jul)","2019-W (15-Jul)"))

I need to append Week column after W in date column我需要在日期列中的W之后的 append 周列

expecteddf <- data.frame(
       'Week' = c(27,28,29),
       'date' = c("2019-W27 (01-Jul)","2019-W28 (08-Jul)","2019-W29 (15-Jul)"))

How can I achieve this in R?如何在 R 中实现这一点?

Thanks in advance!!提前致谢!!

You can use paste0 with a combination of sub , ie您可以将paste0sub组合使用,即

 paste0(sub(' .*', '', df$date), df$Week, sub('.* ', ' ', df$date))
#[1] "2019-W27 (01-Jul)" "2019-W28 (08-Jul)" "2019-W29 (15-Jul)"

in base R, you could also use regmatches + regexpr check the solution @Darren for elaboration on the pattern (?<=W)在基础 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)

With stringr::str_replace , the replacement can be vectorized:使用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)

Alternately, we could take a date formatting approach.或者,我们可以采用日期格式化方法。 Converting your date column to an actual Date class ( df$Date , below), we can then convert the actual Date to your desired format (or any other).将您的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)

A base solution with sub(..., perl = T) :具有sub(..., perl = T)base解决方案:

within(df, date <- Vectorize(sub)("(?<=W)", Week, date, perl = T))

Note:笔记:

  • "(?<=W)" matches the position behind "W" . "(?<=W)"匹配"W"后面的 position 。
  • The first two arguments of sub() cannot be vectorized, so Vectorize() or mapply() are needed here. sub()的前两个arguments不能向量化,所以这里需要Vectorize()或者mapply()

The corresponding str_replace() version, which is vectorized.相应的str_replace()版本,它是矢量化的。

library(dplyr)
library(stringr)

df %>%
  mutate(date = str_replace(date, "(?<=W)", as.character(Week)))

Output Output

#   Week              date
# 1   27 2019-W27 (01-Jul)
# 2   28 2019-W28 (08-Jul)
# 3   29 2019-W29 (15-Jul)

A base R option using:基本 R 选项使用:

  • gsub + Vectorize gsub + Vectorize
expecteddf <- within(df,date <- Vectorize(gsub)("W",paste0("W",Week),date))
  • gsub + mapply gsub + mapply
expecteddf <- within(
  df,
  date <- mapply(function(x, p) gsub("(.*W)(\\s.*)", sprintf("\\1%s\\2", p), x), date, Week)
)

You can use mutate in str_c您可以在 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.

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