Remove everything after a string in a data frame column with missing values

前端 未结 1 2003
花落未央
花落未央 2020-12-20 02:03

I have a data frame resembling the extract below:

Observation Identifier   Value
Obs001      ABC_2001     54
Obs002      ABC_2002     -2
Obs003                       


        
相关标签:
1条回答
  • 2020-12-20 02:24

    You could try the below sub command to remove all the non-space characters from _ symbol.

    sub("_\\S*", "", string)
    

    Explanation:

    • _ Matches a literal _ symbol.
    • \S* Matches zero or more non-space characters.

    OR

    This would remove all the characters from _ symbol,

    sub("_.*", "", string)
    

    Explanation:

    • _ Matches a literal _ symbol.
    • .* Matches any character zero or more times.
    0 讨论(0)
提交回复
热议问题