Trim part of a string in dataframe

后端 未结 3 517
独厮守ぢ
独厮守ぢ 2020-12-21 13:21

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
         


        
3条回答
  •  梦毁少年i
    2020-12-21 13:37

    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))
    

提交回复
热议问题