Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove characters after dash in a mixed string in R

Tags:

r

data
  V9         V10 V11    
1 neutral -0.204 PRKCZ-AS1
2 neutral  0.037 FAAP20-AS1   
3 neutral  0.041 PRKCZ-AS1    
4 neutral  0.041 FAAP20-ZA12   
5 neutral  0.133 PRKCZ-SAS13    
6 neutral  0.133 FAAP20-SAS9999

This is my data frame, a column of which include dash-separated strings. I want to remove the dash and the followed characters. Finally, I want an output as follows:

data
  V9         V10 V11    
1 neutral -0.204 PRKCZ
2 neutral  0.037 FAAP20 
3 neutral  0.041 PRKCZ  
4 neutral  0.041 FAAP20   
5 neutral  0.133 PRKCZ   
6 neutral  0.133 FAAP20

Reproducible sample data

structure(list(V9 = c("neutral", "neutral", "neutral", "neutral", 
"neutral", "neutral"), V10 = c(-0.204, 0.037, 0.041, 0.041, 0.133, 
0.133), V11 = c("PRKCZ-AS1", "FAAP20-AS1", "PRKCZ-AS1", "FAAP20-ZA12", 
"PRKCZ-SAS13", "FAAP20-SAS9999")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

Any help is appreciated.

like image 832
eiuc24 Avatar asked Dec 08 '25 15:12

eiuc24


1 Answers

We can use sub from base R

data$V11 <- sub("-.*", "", data$V11)

Or with trimws

data$V11 <- trimws(data$V11, whitespace = '-.*')
like image 118
akrun Avatar answered Dec 10 '25 03:12

akrun