R 在不同的文件中随机添加一封信
我有一些.txt文件,我需要在每个文件中随机添加字母。我正在尝试此函数,但没有结果 有没有更好的办法R 在不同的文件中随机添加一封信,r,cat,R,Cat,我有一些.txt文件,我需要在每个文件中随机添加字母。我正在尝试此函数,但没有结果 有没有更好的办法 files<-list.files(path=MyDir.,pattern=".txt",full.names = T) for(f in files){ letter<-sample(LETTERS,1) cat(letter,file = f ) } 这行吗 # Make some fake files for(i in 1:3){ write(x =
files<-list.files(path=MyDir.,pattern=".txt",full.names = T)
for(f in files){
letter<-sample(LETTERS,1)
cat(letter,file = f )
}
这行吗
# Make some fake files
for(i in 1:3){
write(x = c(), file = paste0(i,".txt") )
}
# Store the name of the files
( files <- list.files(path=getwd(),pattern=".txt",full.names = T))
# Iterate over the file names and add a letter
for(i in 1:length(files)){
fileConn<-file(files[i])
writeLines(sample(LETTERS,1), fileConn)
close(fileConn)
}
你说我需要在每个文件中随机添加字母是什么意思?添加到实际文件还是仅添加文件名?这不是清晰的添加到实际文件中,我在MyDir中有这些文件:1.txt,2.txt,3.txt,…,100.txt,我使用什么样的函数cai来做这些?你能具体告诉我们发生了什么/什么不起作用吗?catletter,\n,file=f,append=TRUE对我来说似乎正常。R中没有显示错误或警告,但当我打开文件1.txt,2.txt…100.txt时,它们是空的。您尝试过我的修改吗?