user6131384
user6131384

Reputation:

Remove a suffix to a value of a data frame

I have a data frame done this way:

 a       b         c
--------------------------------
 1     2011     mal ID9     
 2     2012     yesterday ID10 
 3     2010     misch ID10 
 4     1995     ship ID9 
 5     2008     se ID9 
 6     1998     falling ID10 
 7     2011     friend ID9 
 8     2011     use to be ID10 
       ...

What I want is removed the ID9and ID10 suffix. The part of the string that precedes ID9 and ID10 has arbitrarily length so I don't know a priori.

For a reproducible example, this is my dataframe:

z <- data.frame(a = c(1,2,3,4,5,6,7,8),
                b = c(2011,2012,2010,1995,2008,1998,2011,2011),
                c = c("mal ID9", "yesterday ID10", "misch ID10", "mal ID10", "se ID9", "falling ID10", "friend ID9", "use to be ID10"))

and this is the result I want:

zz <- data.frame(a = c(1,2,3,4,5,6,7,8),
                b = c(2011,2012,2010,1995,2008,1998,2011,2011),
                c = c("mal", "yesterday", "misch", "mal", "se", "falling", "friend", "use to be"))

How can I do this?

Upvotes: 2

Views: 7252

Answers (2)

Samar.y
Samar.y

Reputation: 136

This should work

    z$c=gsub(" ID.*","",z$c)

Upvotes: 7

akuiper
akuiper

Reputation: 214957

You can try something like this:

z %>% mutate(c = gsub("\\sID\\d+$", "", c))

  a    b         c
1 1 2011       mal
2 2 2012 yesterday
3 3 2010     misch
4 4 1995       mal
5 5 2008        se
6 6 1998   falling
7 7 2011    friend
8 8 2011 use to be

Upvotes: 1

Related Questions