R: loop through objects in global environment and write table in multiple file names
I have been googling for hours but still could not figure this out. I am looping through some objects in R global environment and save it into different file names. But an error always pops up in the final stage:
Error in as.vector(x, mode = "character") :
no method for coercing this S4 class to a vector
The idea is to take all the lists in the environment that starts with proj2, and eventually save them to a txt file. I guess it is because the file name to save also contains x that is directing my object names? But how do I save my files to different names without doing this?
Here is my codes:
savelist <- function(x){
x_up <- subset(x, padj <= 0.05 & log2FoldChange >= 0 )
x_up$fac_lab <- rep("treated_up", nrow(x_up))
x_f_up <- as.data.frame(x_up)[,c(7,2,6)]
write.table(x_f_up, file= paste(x, "up", sep ="_"), sep="t",quote=FALSE, row.names=TRUE, col.names = FALSE, append = FALSE)
}
lapply(mget(ls(pattern = "proj2")), savelist)
Any suggestions will be helpful. Thank you.
r loops lapply
add a comment |
I have been googling for hours but still could not figure this out. I am looping through some objects in R global environment and save it into different file names. But an error always pops up in the final stage:
Error in as.vector(x, mode = "character") :
no method for coercing this S4 class to a vector
The idea is to take all the lists in the environment that starts with proj2, and eventually save them to a txt file. I guess it is because the file name to save also contains x that is directing my object names? But how do I save my files to different names without doing this?
Here is my codes:
savelist <- function(x){
x_up <- subset(x, padj <= 0.05 & log2FoldChange >= 0 )
x_up$fac_lab <- rep("treated_up", nrow(x_up))
x_f_up <- as.data.frame(x_up)[,c(7,2,6)]
write.table(x_f_up, file= paste(x, "up", sep ="_"), sep="t",quote=FALSE, row.names=TRUE, col.names = FALSE, append = FALSE)
}
lapply(mget(ls(pattern = "proj2")), savelist)
Any suggestions will be helpful. Thank you.
r loops lapply
2
The error says that one of the objects you are trying to save to a text file is an S4 class and can't be converted into a data structure like a vector or data frame. It's hard to help without more info about what exactly that object is, but it's not surprising to me that an S4 object might not be easily transformed.
– joran
Nov 14 '18 at 16:05
Look atlapply(mget(ls(pattern = "proj2")), is.data.frame)and verify that each of those objects are actually data.frames. (If they are not, you'll likely need some other function thanwrite.tablesince the object isn't tabular).
– MrFlick
Nov 14 '18 at 16:59
Thanks for your comments! All the objects are converted to data.frames except the x in the last linefile= paste(x, "up", sep ="_")In here the x is a S4 type object but what I am aiming for is to save the file that starts with the same name as each x. What is the solution here?
– UgglaY
Nov 16 '18 at 14:02
add a comment |
I have been googling for hours but still could not figure this out. I am looping through some objects in R global environment and save it into different file names. But an error always pops up in the final stage:
Error in as.vector(x, mode = "character") :
no method for coercing this S4 class to a vector
The idea is to take all the lists in the environment that starts with proj2, and eventually save them to a txt file. I guess it is because the file name to save also contains x that is directing my object names? But how do I save my files to different names without doing this?
Here is my codes:
savelist <- function(x){
x_up <- subset(x, padj <= 0.05 & log2FoldChange >= 0 )
x_up$fac_lab <- rep("treated_up", nrow(x_up))
x_f_up <- as.data.frame(x_up)[,c(7,2,6)]
write.table(x_f_up, file= paste(x, "up", sep ="_"), sep="t",quote=FALSE, row.names=TRUE, col.names = FALSE, append = FALSE)
}
lapply(mget(ls(pattern = "proj2")), savelist)
Any suggestions will be helpful. Thank you.
r loops lapply
I have been googling for hours but still could not figure this out. I am looping through some objects in R global environment and save it into different file names. But an error always pops up in the final stage:
Error in as.vector(x, mode = "character") :
no method for coercing this S4 class to a vector
The idea is to take all the lists in the environment that starts with proj2, and eventually save them to a txt file. I guess it is because the file name to save also contains x that is directing my object names? But how do I save my files to different names without doing this?
Here is my codes:
savelist <- function(x){
x_up <- subset(x, padj <= 0.05 & log2FoldChange >= 0 )
x_up$fac_lab <- rep("treated_up", nrow(x_up))
x_f_up <- as.data.frame(x_up)[,c(7,2,6)]
write.table(x_f_up, file= paste(x, "up", sep ="_"), sep="t",quote=FALSE, row.names=TRUE, col.names = FALSE, append = FALSE)
}
lapply(mget(ls(pattern = "proj2")), savelist)
Any suggestions will be helpful. Thank you.
r loops lapply
r loops lapply
asked Nov 14 '18 at 15:55
UgglaYUgglaY
11
11
2
The error says that one of the objects you are trying to save to a text file is an S4 class and can't be converted into a data structure like a vector or data frame. It's hard to help without more info about what exactly that object is, but it's not surprising to me that an S4 object might not be easily transformed.
– joran
Nov 14 '18 at 16:05
Look atlapply(mget(ls(pattern = "proj2")), is.data.frame)and verify that each of those objects are actually data.frames. (If they are not, you'll likely need some other function thanwrite.tablesince the object isn't tabular).
– MrFlick
Nov 14 '18 at 16:59
Thanks for your comments! All the objects are converted to data.frames except the x in the last linefile= paste(x, "up", sep ="_")In here the x is a S4 type object but what I am aiming for is to save the file that starts with the same name as each x. What is the solution here?
– UgglaY
Nov 16 '18 at 14:02
add a comment |
2
The error says that one of the objects you are trying to save to a text file is an S4 class and can't be converted into a data structure like a vector or data frame. It's hard to help without more info about what exactly that object is, but it's not surprising to me that an S4 object might not be easily transformed.
– joran
Nov 14 '18 at 16:05
Look atlapply(mget(ls(pattern = "proj2")), is.data.frame)and verify that each of those objects are actually data.frames. (If they are not, you'll likely need some other function thanwrite.tablesince the object isn't tabular).
– MrFlick
Nov 14 '18 at 16:59
Thanks for your comments! All the objects are converted to data.frames except the x in the last linefile= paste(x, "up", sep ="_")In here the x is a S4 type object but what I am aiming for is to save the file that starts with the same name as each x. What is the solution here?
– UgglaY
Nov 16 '18 at 14:02
2
2
The error says that one of the objects you are trying to save to a text file is an S4 class and can't be converted into a data structure like a vector or data frame. It's hard to help without more info about what exactly that object is, but it's not surprising to me that an S4 object might not be easily transformed.
– joran
Nov 14 '18 at 16:05
The error says that one of the objects you are trying to save to a text file is an S4 class and can't be converted into a data structure like a vector or data frame. It's hard to help without more info about what exactly that object is, but it's not surprising to me that an S4 object might not be easily transformed.
– joran
Nov 14 '18 at 16:05
Look at
lapply(mget(ls(pattern = "proj2")), is.data.frame) and verify that each of those objects are actually data.frames. (If they are not, you'll likely need some other function than write.table since the object isn't tabular).– MrFlick
Nov 14 '18 at 16:59
Look at
lapply(mget(ls(pattern = "proj2")), is.data.frame) and verify that each of those objects are actually data.frames. (If they are not, you'll likely need some other function than write.table since the object isn't tabular).– MrFlick
Nov 14 '18 at 16:59
Thanks for your comments! All the objects are converted to data.frames except the x in the last line
file= paste(x, "up", sep ="_") In here the x is a S4 type object but what I am aiming for is to save the file that starts with the same name as each x. What is the solution here?– UgglaY
Nov 16 '18 at 14:02
Thanks for your comments! All the objects are converted to data.frames except the x in the last line
file= paste(x, "up", sep ="_") In here the x is a S4 type object but what I am aiming for is to save the file that starts with the same name as each x. What is the solution here?– UgglaY
Nov 16 '18 at 14:02
add a comment |
0
active
oldest
votes
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%2f53304149%2fr-loop-through-objects-in-global-environment-and-write-table-in-multiple-file-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53304149%2fr-loop-through-objects-in-global-environment-and-write-table-in-multiple-file-n%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
2
The error says that one of the objects you are trying to save to a text file is an S4 class and can't be converted into a data structure like a vector or data frame. It's hard to help without more info about what exactly that object is, but it's not surprising to me that an S4 object might not be easily transformed.
– joran
Nov 14 '18 at 16:05
Look at
lapply(mget(ls(pattern = "proj2")), is.data.frame)and verify that each of those objects are actually data.frames. (If they are not, you'll likely need some other function thanwrite.tablesince the object isn't tabular).– MrFlick
Nov 14 '18 at 16:59
Thanks for your comments! All the objects are converted to data.frames except the x in the last line
file= paste(x, "up", sep ="_")In here the x is a S4 type object but what I am aiming for is to save the file that starts with the same name as each x. What is the solution here?– UgglaY
Nov 16 '18 at 14:02