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;
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
add a comment |
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
Useget
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 listlapply()
or whatever over the list.
– MrFlick
Mar 27 at 20:10
add a comment |
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
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
r
asked Mar 27 at 19:34
ASFASF
877 bronze badges
877 bronze badges
Useget
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 listlapply()
or whatever over the list.
– MrFlick
Mar 27 at 20:10
add a comment |
Useget
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 listlapply()
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
add a comment |
2 Answers
2
active
oldest
votes
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)
add a comment |
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Mar 27 at 19:42
Leo BrueggemanLeo Brueggeman
3531 silver badge4 bronze badges
3531 silver badge4 bronze badges
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Mar 27 at 20:16
fran496fran496
861 silver badge4 bronze badges
861 silver badge4 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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