2017-09-25 5 views
0

我々はRに非常に簡単にデータ行列を扱うことができますが、ここで私は以下のように私のループからプリントアウトした文字列を持っている:ループから文字出力を保存し、Rにテキストファイルとして書き込む方法は?

for (i in 1:length(mylength)){ 
DO something to get my_string until length(mylength) 
cat(my_string) 
collection <- ##how can I save the my_string one after another leaving one line gap so I can write everything (collection) using the code below? 
} 

私ができるので、私は別残して1つのラインギャップの後my_string 1を保存することができますどのように以下のコードを使用してすべてを書き込んでください(collection)?

collection <- "" 

for (i in 1:length(mylength)) { 
    # get my_string 
    collection <- paste0(collection, "\n") 
} 

cat(collection) 

print()ことは、これらの改行は表示されませんが、catはすべき:

##writing the collected file 
cat(collection, file= "All_colected_mystring.txt") 
+0

代わりに、代わりに '' purr''の反復関数を使うことができます。その場合は 'map_chr'を使います。 –

答えて

1

あなたは改行で区切られmylength文字列の各部分を、連結するpaste0()を使用することができます。

関連する問題