Input `typ` can't be recycled to size in R

Viewed 14119

I have this data frame:

DF
V1  V2 
P1  03.02.2020
22  04.02.2020
33  05.02.2020
P2  05.02.2020
P1  06.02.2020 

And I would like to have this output

DF
V1  V2           V3
P1  03.02.2020   P1
22  04.02.2020   NA
33  05.02.2020   NA
R2  05.02.2020   R2
S3  06.02.2020   S3

In V3 I would like to have only string contains a letter from V1.

I used this code

DF %>% mutate(V3 = grep("([A-Z])", V1))

But I got this error:

x Input typ can't be recycled to size

How could I fix this?

2 Answers

You can use ifelse:

DF %>% mutate(V3 = ifelse(grepl("[A-Z]", V1), as.character(V1), NA))
#>   V1         V2   V3
#> 1 P1 03.02.2020   P1
#> 2 22 04.02.2020 <NA>
#> 3 33 05.02.2020 <NA>
#> 4 P2 05.02.2020   P2
#> 5 P1 06.02.2020   P1

You can overwrite those which have no match:

DF$V3 <- DF$V1
DF$V3[!grepl("([A-Z])", DF$V1)] <- NA
#DF$V3[-grep("([A-Z])", DF$V1)] <- NA #Alternative
DF
#  V1         V2   V3
#1 P1 03.02.2020   P1
#2 22 04.02.2020 <NA>
#3 33 05.02.2020 <NA>
#4 P2 05.02.2020   P2
#5 P1 06.02.2020   P1
Related