3

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?

oguz ismail
  • 1
  • 16
  • 47
  • 69
onhalu
  • 735
  • 1
  • 5
  • 17

2 Answers2

3

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
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

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
GKi
  • 37,245
  • 2
  • 26
  • 48