-5

Screenshot of the dataframe

I want to replace everything after the first _ in data77298$SAMPLE.CODE to " ", such that I get levels to be GSM2048265, GSM2048266 etc.,

Is it possible using a single command to change all strings after the underscore to null?

Harryy
  • 41
  • 9
  • Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Perhaps the following tips on [asking a good question](http://stackoverflow.com/help/how-to-ask) may also be worth a read. – lmo Jun 29 '16 at 18:27
  • It helps to post the example input text directly in your question – copeg Jun 29 '16 at 18:29

2 Answers2

1

you can do it by gsub

my_string<-c("GSM2048265_Somet_323_h4554ing_here","GSM2048266_sometwewe_sdsd_hing_here")
gsub("\\_.*","",my_string)
[1] "GSM2048265" "GSM2048266"
user5249203
  • 4,436
  • 1
  • 19
  • 45
0

How about:

library(stringr)
my_string<-c("GSM2048265_1_2_£_$_F_CA","GSM2048266_aasv_vaerv_vasd", "GSM2048266_arvqb_oyor_1234")
word(my_string, 1, sep = "_")
thepule
  • 1,721
  • 1
  • 12
  • 22