Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

regex - Trim part of a string in dataframe

If I have a dataframe structure like that:

AA1_123.zip
BB2_456.txt
CCC_789.doc

How can I change it to this:

AA1
BB2
CCC
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could try sub

sub('_.*', '', df1$Col)
#[1] "AA1" "BB2" "CCC"

data

df1 <- structure(list(Col = c("AA1_123.zip", "BB2_456.txt", 
"CCC_789.doc"
)), .Names = "Col", class = "data.frame", row.names = c(NA, -3L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...