我有这个df:
df = data.frame(
date = c(01/01/2020, 01/02/2020, 01/03/2020),
day = c("Rain31", "Rain12", "Rain04")
)
date day
1 01/01/2020 Rain31
2 01/02/2020 Rain12
3 01/03/2020 Rain04
我想达到这个目标:
date day true.day
1 01/01/2020 Rain31 31/01/2020
2 01/02/2020 Rain12 12/02/2020
3 01/03/2020 Rain04 01/03/2020
我可以使用split()和unite()
但是现在,我可以只使用基本函数:(
我做了什么:(对于分辨率而言并不重要)
df %>%
separate(day, into = c("rain", "nday"), sep = 4 ) %>%
separate(date, into = c("day", "month_year"), sep = 3) %>%
unite(DataPrecMax, nday, month_year, sep = "")
答案 0 :(得分:2)
您可以从0x9a
列中删除非数字,并替换day
的前2个字符。
date
数据
df$true.date <- df$date
substring(df$true.date, 1, 2) <- sub('\\D+', '', df$day)
df
# date day true.date
#1 01/01/2020 Rain31 31/01/2020
#2 01/02/2020 Rain12 12/02/2020
#3 01/03/2020 Rain04 04/03/2020
答案 1 :(得分:2)
我们可以使用str_replace
library(dplyr)
library(stringr)
df %>%
mutate(true.day = str_replace(date, '..', str_remove(day, 'Rain')))
#. date day true.day
#1 01/01/2020 Rain31 31/01/2020
#2 01/02/2020 Rain12 12/02/2020
#3 01/03/2020 Rain04 04/03/2020
如果我们仅需要base R
个函数
df$true.day <- with(df, sub("^..(\\S+)\\s+\\D+(\\d+)", "\\2\\1", paste(date, day)))
df <- data.frame(
date = c("01/01/2020", "01/02/2020", "01/03/2020"),
day = c("Rain31", "Rain12", "Rain04"), stringsAsFactors = FALSE)