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

后端 未结 2 887
谎友^
谎友^ 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 08:40

    @lukeA has posted the most logical answer to this, but you could also use read.fwf:

    > read.fwf(textConnection(mydf$filname), 3)
       V1
    1 301
    2 302
    3 303
    

    Depending on the str of your data, it might need to be read.fwf(textConnection(as.character(mydf$filname)), 3) if mydf$filename is a factor variable.

提交回复
热议问题