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 A2 A3 301fg.xls
B B2 B3 302gfg.xls
C C2 C3 303gfsddf.xls
.,.,.,.
I want to renamne the names in the filename column to only contain the 3 first characters/digits thus giving:
data1 data2 data3 filname
A A2 A3 301
B B2 B3 302
C C2 C3 303
.,.,.,.
df$filname <- sub("^(\\d{3}).*$", "\\1", df$filname)
要么
df$filname <- substr(df$filname, 0, 3)
@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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.