循环写入多个数据帧

时间:2016-12-20 17:30:40

标签: r loops

我有6-10个数据框对象,我想写出来。现在我必须用var videoSettings = new NSMutableDictionary(); videoSettings.Add(AVVideo.CodecKey, AVVideo.CodecH264); videoSettings.Add(AVVideo.WidthKey, NSNumber.FromNFloat(images[0].Size.Width)); videoSettings.Add(AVVideo.HeightKey, NSNumber.FromNFloat(images[0].Size.Height)); var videoWriter = new AVAssetWriter(fileURL, AVFileType.Mpeg4, out nsError); var writerInput = new AVAssetWriterInput(AVMediaType.Video, new AVVideoSettingsCompressed(videoSettings)); var sourcePixelBufferAttributes = new NSMutableDictionary(); sourcePixelBufferAttributes.Add(CVPixelBuffer.PixelFormatTypeKey, NSNumber.FromInt32((int)CVPixelFormatType.CV32ARGB)); var pixelBufferAdaptor = new AVAssetWriterInputPixelBufferAdaptor(writerInput, sourcePixelBufferAttributes); videoWriter.AddInput(writerInput); if (videoWriter.StartWriting()) { videoWriter.StartSessionAtSourceTime(CMTime.Zero); for (int i = 0; i < images.Length; i++) { while (true) { if (writerInput.ReadyForMoreMediaData) { var frameTime = new CMTime(1, 10); var lastTime = new CMTime(1 * i, 10); var presentTime = CMTime.Add(lastTime, frameTime); var pixelBufferImage = PixelBufferFromCGImage(images[i].CGImage, pixelBufferAdaptor); Console.WriteLine(pixelBufferAdaptor.AppendPixelBufferWithPresentationTime(pixelBufferImage, presentTime)); break; } } } writerInput.MarkAsFinished(); await videoWriter.FinishWritingAsync(); 手动写出每一个。我想简化我的代码并遍历对象并将它们全部写出来。

我在这里尝试过这些建议:

Write to files in R using a loop

writing many files in a for loop using R

但是,我一直在获取正确命名的文件,但看起来像这样:

write.table

以下是我的代码示例:

> "x"  
"1" "listdata_cent"

非常感谢您提供的任何帮助。我确定我错过了一些简单的东西。

1 个答案:

答案 0 :(得分:0)

感谢Nick Criswell,Richard Telford和Gregor。我找到了解决方案。

datalist = lapply(ls(pattern = "^listdata"), get)
names(datalist) <- (ls(pattern = "^listdata"))
for (i in 1:length(datalist)) {
  write.table(datalist[i], file = paste(names(datalist[i]), ".txt", sep = ""), col.names= TRUE, sep = "\t", quote=FALSE)
}
  

看起来这可能与此问题有关。 - Nick Criswell 4小时前   1
  我就是这个名字。得到(i)会找到对象我 - 理查德特尔福德4小时前   1
  而不是让一堆数据帧浮动,最佳做法是拥有一个数据帧列表。如果您的数据框位于列表中,则问题变得微不足道。 - Gregor 4小时前