mydataframe的一个例子是:
datatimedf = data.frame(time_start_1 = c('Apr 2005', '2004', '2000', 'Mar 2011'), time_end_1 = c('Apr 2012', '2011', 'Oct 2001', '2012'))
这些列中的某些值包含年份和月份,其他值仅包含年份。如果一个单元格只有年份(缺少月份),我想插入月份值。
要插入的月份值取决于它所在的列:如果它在time_start_1
列中,请插入" Jan"如果它在time_end_1
列插入" Dec"。
预期产出的例子:
datatimedf = data.frame(time_start_1 = c('Apr 2005', 'Jan 2004', 'Jan 2000', 'Mar 2011'), time_end_1 = c('Apr 2012', 'Dec 2011', 'Oct 2001', 'Dec 2012'))
答案 0 :(得分:0)
如果我的意图正确,只要你有字符列而不是因素(因此stringsAsFactors = F
)
library(dplyr)
library(stringr)
datatimedf = data.frame(time_start_1 = c('Apr 2005', '2004', '2000', 'Mar 2011'), time_end_1 = c('Apr 2012', '2011', 'Oct 2001', '2012'), stringsAsFactors = F)
datatimedf %>%
mutate(time_start_1 = ifelse(str_detect(time_start_1, "[[:alpha:]]"), time_start_1, paste0("Jan ", time_start_1))) %>%
mutate(time_end_1 = ifelse(str_detect(time_end_1, "[[:alpha:]]"), time_end_1, paste0("Dec ", time_end_1)))