Reputation: 311
I have a DF
with column of names
DF
a b
a.b 1
a.c 2
a.d 3
b.c 4
b.l 5
b.d 6
c.a 6
c.b 7
I need to create new column which contains symbol before .
in DF$a
, so that
DF
a b c
a.b 1 a
a.c 2 a
a.d 3 a
b.c 4 b
b.l 5 b
b.d 6 b
c.a 6 c
c.b 7 c
Is it possible? Thank you for any suggestions.
Upvotes: 0
Views: 45
Reputation: 886938
We can use sub
to match the dot character(\\.
) followed by one or more characters to the end of the string and replace with ''
.
DF$c <- sub("\\..*$", "", DF$a)
DF$c
#[1] "a" "a" "a" "b" "b" "b" "c" "c"
Or
library(stringr)
str_extract(DF$a, "\\w+")
Upvotes: 1