Write multiple loaded variables into different .txt filesHow do I make a list of data frames?Inserting a table of data into a text fileError when reading in a .txt file and splitting it into columns in RCombine columns having same column name from multiple data framesR subsetting data.frame with conditions from other data.framesRetrieve/access dynamic variable from R functionWrite multiple TXT files to Excel in RUsing a txt file to name multiple colums in rHow does one send a list of column names as a variable into dplyr's mutate?How to extract values from data.frame conditionally?r - Split dataframe into multiple dataframes and save in environment

What is the loud noise of a helicopter when the rotors are not yet moving?

When one problem is added to the previous one

How do I make my image comply with the requirements of this photography competition?

Could this kind of inaccurate sacrifice be countered?

How do I feed my black hole?

What happened to the HDEV ISS Experiment? Is it over?

How does encoder decoder network works?

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

How to obtain a polynomial with these conditions?

Expressing the act of drawing

Who was the most successful German spy against Great Britain in WWII, from the contemporary German perspective?

Hangman game in Python - need feedback on the quality of code

Does maintaining a spell with a longer casting time count as casting a spell?

Cooking Scrambled Eggs

Why doesn't 'd /= d' throw a division by zero exception?

Why does a sticker slowly peel off, but if it is pulled quickly it tears?

"Opusculum hoc, quamdiu vixero, doctioribus emendandum offero."?

Prevent use of CNAME record for untrusted domain

Tex Quotes(UVa 272)

Does ostensible/specious make sense in this sentence?

Prison offence - trespassing underwood fence

Boot Windows from SAN

What stops you from using fixed income in developing countries?

Heyacrazy: Empty Space



Write multiple loaded variables into different .txt files


How do I make a list of data frames?Inserting a table of data into a text fileError when reading in a .txt file and splitting it into columns in RCombine columns having same column name from multiple data framesR subsetting data.frame with conditions from other data.framesRetrieve/access dynamic variable from R functionWrite multiple TXT files to Excel in RUsing a txt file to name multiple colums in rHow does one send a list of column names as a variable into dplyr's mutate?How to extract values from data.frame conditionally?r - Split dataframe into multiple dataframes and save in environment






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I need to write multiple variables (dataframes) into different .txt files, named based it's original variables names. I tried use ls() function to select by pattern my desirable variables, but with no success. Is there any other approach to do this?



Using ls() function I was able to create .txt files with the correct filenames based on my variables (data1_tables.txt, data2_tables.txt, etc), but with the wrong output.



#create some variables based on mtcars data
data1 <- mtcars[1:5,]
data2 <- mtcars[6:10,]
data3 <- mtcars[11:20,]



fileNames=ls(pattern="data",all.names=TRUE)


for (i in fileNames)
write.table(i,paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)



I want that the created files (data1_tables.txt, data2_tables.txt, data3_tables.txt) have the output from the original data1, data2, data3 variables.










share|improve this question
























  • Use get to reference object by string: write.table(get(i), paste(...

    – Parfait
    Mar 27 at 19:56






  • 1





    It's better not to create a much of objects with suffixes in their names. Things are much easier in R if you just collect all those objects in a named list. See this question for an example: stackoverflow.com/questions/17499013/…. Then you can list lapply() or whatever over the list.

    – MrFlick
    Mar 27 at 20:10

















3















I need to write multiple variables (dataframes) into different .txt files, named based it's original variables names. I tried use ls() function to select by pattern my desirable variables, but with no success. Is there any other approach to do this?



Using ls() function I was able to create .txt files with the correct filenames based on my variables (data1_tables.txt, data2_tables.txt, etc), but with the wrong output.



#create some variables based on mtcars data
data1 <- mtcars[1:5,]
data2 <- mtcars[6:10,]
data3 <- mtcars[11:20,]



fileNames=ls(pattern="data",all.names=TRUE)


for (i in fileNames)
write.table(i,paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)



I want that the created files (data1_tables.txt, data2_tables.txt, data3_tables.txt) have the output from the original data1, data2, data3 variables.










share|improve this question
























  • Use get to reference object by string: write.table(get(i), paste(...

    – Parfait
    Mar 27 at 19:56






  • 1





    It's better not to create a much of objects with suffixes in their names. Things are much easier in R if you just collect all those objects in a named list. See this question for an example: stackoverflow.com/questions/17499013/…. Then you can list lapply() or whatever over the list.

    – MrFlick
    Mar 27 at 20:10













3












3








3








I need to write multiple variables (dataframes) into different .txt files, named based it's original variables names. I tried use ls() function to select by pattern my desirable variables, but with no success. Is there any other approach to do this?



Using ls() function I was able to create .txt files with the correct filenames based on my variables (data1_tables.txt, data2_tables.txt, etc), but with the wrong output.



#create some variables based on mtcars data
data1 <- mtcars[1:5,]
data2 <- mtcars[6:10,]
data3 <- mtcars[11:20,]



fileNames=ls(pattern="data",all.names=TRUE)


for (i in fileNames)
write.table(i,paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)



I want that the created files (data1_tables.txt, data2_tables.txt, data3_tables.txt) have the output from the original data1, data2, data3 variables.










share|improve this question














I need to write multiple variables (dataframes) into different .txt files, named based it's original variables names. I tried use ls() function to select by pattern my desirable variables, but with no success. Is there any other approach to do this?



Using ls() function I was able to create .txt files with the correct filenames based on my variables (data1_tables.txt, data2_tables.txt, etc), but with the wrong output.



#create some variables based on mtcars data
data1 <- mtcars[1:5,]
data2 <- mtcars[6:10,]
data3 <- mtcars[11:20,]



fileNames=ls(pattern="data",all.names=TRUE)


for (i in fileNames)
write.table(i,paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)



I want that the created files (data1_tables.txt, data2_tables.txt, data3_tables.txt) have the output from the original data1, data2, data3 variables.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 19:34









ASFASF

877 bronze badges




877 bronze badges















  • Use get to reference object by string: write.table(get(i), paste(...

    – Parfait
    Mar 27 at 19:56






  • 1





    It's better not to create a much of objects with suffixes in their names. Things are much easier in R if you just collect all those objects in a named list. See this question for an example: stackoverflow.com/questions/17499013/…. Then you can list lapply() or whatever over the list.

    – MrFlick
    Mar 27 at 20:10

















  • Use get to reference object by string: write.table(get(i), paste(...

    – Parfait
    Mar 27 at 19:56






  • 1





    It's better not to create a much of objects with suffixes in their names. Things are much easier in R if you just collect all those objects in a named list. See this question for an example: stackoverflow.com/questions/17499013/…. Then you can list lapply() or whatever over the list.

    – MrFlick
    Mar 27 at 20:10
















Use get to reference object by string: write.table(get(i), paste(...

– Parfait
Mar 27 at 19:56





Use get to reference object by string: write.table(get(i), paste(...

– Parfait
Mar 27 at 19:56




1




1





It's better not to create a much of objects with suffixes in their names. Things are much easier in R if you just collect all those objects in a named list. See this question for an example: stackoverflow.com/questions/17499013/…. Then you can list lapply() or whatever over the list.

– MrFlick
Mar 27 at 20:10





It's better not to create a much of objects with suffixes in their names. Things are much easier in R if you just collect all those objects in a named list. See this question for an example: stackoverflow.com/questions/17499013/…. Then you can list lapply() or whatever over the list.

– MrFlick
Mar 27 at 20:10












2 Answers
2






active

oldest

votes


















1















Change the end of your code to:



for (i in fileNames) 
write.table(eval(as.name(i)),paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)







share|improve this answer
































    2















    What is happenning is that, what you're actually writing to files are the elements from the fileNames vector (which are just strings). If you want to write any object to a file through the write functions, you need to input the object itself, not the name of the object.



    #create some variables based on mtcars data
    data1 <- mtcars[1:5,]
    data2 <- mtcars[6:10,]
    data3 <- mtcars[11:20,]

    fileNames = ls(pattern="data", all.names=TRUE)

    for(i in fileNames)

    write.table(x=get(i), # The get function gets an object with a given name.
    file=paste0(i, "_tables.txt"), # paste0 is basically a paste with sep="" by default
    row.names=T,
    sep="t",
    quote=F)






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385178%2fwrite-multiple-loaded-variables-into-different-txt-files%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1















      Change the end of your code to:



      for (i in fileNames) 
      write.table(eval(as.name(i)),paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)







      share|improve this answer





























        1















        Change the end of your code to:



        for (i in fileNames) 
        write.table(eval(as.name(i)),paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)







        share|improve this answer



























          1














          1










          1









          Change the end of your code to:



          for (i in fileNames) 
          write.table(eval(as.name(i)),paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)







          share|improve this answer













          Change the end of your code to:



          for (i in fileNames) 
          write.table(eval(as.name(i)),paste(i,"_tables.txt",sep=""),row.names = T,sep="t",quote=F)








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 19:42









          Leo BrueggemanLeo Brueggeman

          3531 silver badge4 bronze badges




          3531 silver badge4 bronze badges


























              2















              What is happenning is that, what you're actually writing to files are the elements from the fileNames vector (which are just strings). If you want to write any object to a file through the write functions, you need to input the object itself, not the name of the object.



              #create some variables based on mtcars data
              data1 <- mtcars[1:5,]
              data2 <- mtcars[6:10,]
              data3 <- mtcars[11:20,]

              fileNames = ls(pattern="data", all.names=TRUE)

              for(i in fileNames)

              write.table(x=get(i), # The get function gets an object with a given name.
              file=paste0(i, "_tables.txt"), # paste0 is basically a paste with sep="" by default
              row.names=T,
              sep="t",
              quote=F)






              share|improve this answer





























                2















                What is happenning is that, what you're actually writing to files are the elements from the fileNames vector (which are just strings). If you want to write any object to a file through the write functions, you need to input the object itself, not the name of the object.



                #create some variables based on mtcars data
                data1 <- mtcars[1:5,]
                data2 <- mtcars[6:10,]
                data3 <- mtcars[11:20,]

                fileNames = ls(pattern="data", all.names=TRUE)

                for(i in fileNames)

                write.table(x=get(i), # The get function gets an object with a given name.
                file=paste0(i, "_tables.txt"), # paste0 is basically a paste with sep="" by default
                row.names=T,
                sep="t",
                quote=F)






                share|improve this answer



























                  2














                  2










                  2









                  What is happenning is that, what you're actually writing to files are the elements from the fileNames vector (which are just strings). If you want to write any object to a file through the write functions, you need to input the object itself, not the name of the object.



                  #create some variables based on mtcars data
                  data1 <- mtcars[1:5,]
                  data2 <- mtcars[6:10,]
                  data3 <- mtcars[11:20,]

                  fileNames = ls(pattern="data", all.names=TRUE)

                  for(i in fileNames)

                  write.table(x=get(i), # The get function gets an object with a given name.
                  file=paste0(i, "_tables.txt"), # paste0 is basically a paste with sep="" by default
                  row.names=T,
                  sep="t",
                  quote=F)






                  share|improve this answer













                  What is happenning is that, what you're actually writing to files are the elements from the fileNames vector (which are just strings). If you want to write any object to a file through the write functions, you need to input the object itself, not the name of the object.



                  #create some variables based on mtcars data
                  data1 <- mtcars[1:5,]
                  data2 <- mtcars[6:10,]
                  data3 <- mtcars[11:20,]

                  fileNames = ls(pattern="data", all.names=TRUE)

                  for(i in fileNames)

                  write.table(x=get(i), # The get function gets an object with a given name.
                  file=paste0(i, "_tables.txt"), # paste0 is basically a paste with sep="" by default
                  row.names=T,
                  sep="t",
                  quote=F)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 27 at 20:16









                  fran496fran496

                  861 silver badge4 bronze badges




                  861 silver badge4 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385178%2fwrite-multiple-loaded-variables-into-different-txt-files%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현