R: Keep only 3 (x) first characters in all rows in a column?

I imported several thousand xls files to data.frame and I added a column with the file name.

So I have data

data1  data2  data3  filname
A      A2     A3     301fg.xls
B      B2     B3     302gfg.xls
C      C2     C3     303gfsddf.xls
.,.,.,.

I want to rename the names in the file name column to contain only the first 3 characters / digits, which gives:

data1  data2  data3  filname
A      A2     A3     301
B      B2     B3     302
C      C2     C3     303
.,.,.,.
+4
source share
2 answers
df$filname <- sub("^(\\d{3}).*$", "\\1", df$filname)

or

df$filname <- substr(df$filname, 0, 3)
+13
source

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

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

Depending on stryour data, it may be required read.fwf(textConnection(as.character(mydf$filname)), 3)if it mydf$filenameis a variable factor.

0
source

Source: https://habr.com/ru/post/1526146/


All Articles