[英]R: Only keep the 3 (x) first characters in a all rows in a column?
I have imported a few thousand xls files into a data.frame and I added a column with the filename. 我已经将数千个xls文件导入到data.frame中,并添加了带有文件名的列。
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: 我想重命名文件名列中的名称,使其仅包含3个首字符/数字,从而得到:
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
: @lukeA发布了对此的最合乎逻辑的答案,但您也可以使用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. 根据数据的str
,可能需要读取它read.fwf(textConnection(as.character(mydf$filname)), 3)
如果mydf$filename
是一个factor
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.