0

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.

  • You can't use an `if` statement on a vector that way. `df$pos1 == 3` will return a whole vector of TRUE/FALSE values, but an `if` can only handle a single TRUE or FALSE. In any case, you are doing this the hard way. `grep("\\d{4}", df$Date, value = TRUE)` will grab all the years without any conditional statements. – Allan Cameron Jun 18 '22 at 19:33

1 Answers1

1

You can try use ifelse the vectorized version of if

df$Year1 <- ifelse(df$pos1 == 3 ,  str_sub(df$Date,-4,-1) ,
 str_sub(df$Date, 1, 4))
Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19