I tried to execute a simple if statement and got this warning "the condition has length > 1 and only the first element will be used". As a result, I think the Year1 field is not getting created properly.
library(stringi)
library(stringr)
df <- data.frame(Date = c("19-9-2019","2021-9-20","2020-22-12", "18-9-2016"))
df$pos1 <- str_locate(df$Date, "-")[,1] # get the position of the first occurrence for "-"
if(df$pos1 == 3)
{
df$Year1 = str_sub(df$Date,-4,-1)
}
if(df$pos1 == 5)
{
df$Year1 = str_sub(df$Date, 1, 4)
}
I also tried using else statement instead of two if statements but that didn't help. There are few posts on similar issue but their resolution didn't work for me.
Please tell how to correct the error and get the right year field for my data. Thanks.