R: Only keep the 3 (x) first characters in a all rows in a column?

后端 未结 2 888
谎友^
谎友^ 2021-02-07 08:33

I have imported a few thousand xls files into a data.frame and I added a column with the filename.

Thus I have the data

data1  data2  data3  filname
A          


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 08:50

    df$filname <- sub("^(\\d{3}).*$", "\\1", df$filname)
    

    or

    df$filname <- substr(df$filname, 0, 3)
    

提交回复
热议问题