How can I avoid “$ operator is invalid for atomic vectors”
I have a loop running to assign values
for (i in 1:4)
t[i]<-print(noquote(paste0("t_",i)))
the output of which is
[1] t_1
[1] t_2
[1] t_3
[1] t_4
Now when I try to run it along with a function
for (i in 1:length(t))
print(sum(t[i]$total_counts))
it throws up an error: Error: $ operator is invalid for atomic vectors
however, it works fine, if i run it like
sum(t_1$total_counts)
[1] 27347116
what shall I adjust to make my code running in a loop.
Please suggest.
r
add a comment |
I have a loop running to assign values
for (i in 1:4)
t[i]<-print(noquote(paste0("t_",i)))
the output of which is
[1] t_1
[1] t_2
[1] t_3
[1] t_4
Now when I try to run it along with a function
for (i in 1:length(t))
print(sum(t[i]$total_counts))
it throws up an error: Error: $ operator is invalid for atomic vectors
however, it works fine, if i run it like
sum(t_1$total_counts)
[1] 27347116
what shall I adjust to make my code running in a loop.
Please suggest.
r
2
mtcars$cyl[1]
is a valid reference,mtcars[1]$cyl
is not
– Mako212
Nov 20 '18 at 22:53
add a comment |
I have a loop running to assign values
for (i in 1:4)
t[i]<-print(noquote(paste0("t_",i)))
the output of which is
[1] t_1
[1] t_2
[1] t_3
[1] t_4
Now when I try to run it along with a function
for (i in 1:length(t))
print(sum(t[i]$total_counts))
it throws up an error: Error: $ operator is invalid for atomic vectors
however, it works fine, if i run it like
sum(t_1$total_counts)
[1] 27347116
what shall I adjust to make my code running in a loop.
Please suggest.
r
I have a loop running to assign values
for (i in 1:4)
t[i]<-print(noquote(paste0("t_",i)))
the output of which is
[1] t_1
[1] t_2
[1] t_3
[1] t_4
Now when I try to run it along with a function
for (i in 1:length(t))
print(sum(t[i]$total_counts))
it throws up an error: Error: $ operator is invalid for atomic vectors
however, it works fine, if i run it like
sum(t_1$total_counts)
[1] 27347116
what shall I adjust to make my code running in a loop.
Please suggest.
r
r
asked Nov 20 '18 at 22:50
AngeloAngelo
1,66962345
1,66962345
2
mtcars$cyl[1]
is a valid reference,mtcars[1]$cyl
is not
– Mako212
Nov 20 '18 at 22:53
add a comment |
2
mtcars$cyl[1]
is a valid reference,mtcars[1]$cyl
is not
– Mako212
Nov 20 '18 at 22:53
2
2
mtcars$cyl[1]
is a valid reference, mtcars[1]$cyl
is not– Mako212
Nov 20 '18 at 22:53
mtcars$cyl[1]
is a valid reference, mtcars[1]$cyl
is not– Mako212
Nov 20 '18 at 22:53
add a comment |
1 Answer
1
active
oldest
votes
I assume that t_1
to t_n
are the names of objects in your environment and that you want to loop over said objects and perform an operation. in which case you need to use get
:
t_1 <- data.frame(x=1:10, y = 11:20)
t <- "t_1"
sum(get(t[1])$x)
#[1] 55
As @joran suggests, the better way to do this is to collect all of the t_n
objects into a list and then operate over the list.
t_list <- mget(t)
lapply(t_list, function(df) sum(df$x))
2
Rather than encourage this somewhat problematic code pattern, you could recommendmget
as a way to collect all thoset_*
objects into a named list.
– joran
Nov 20 '18 at 23:06
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%2f53402771%2fhow-can-i-avoid-operator-is-invalid-for-atomic-vectors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume that t_1
to t_n
are the names of objects in your environment and that you want to loop over said objects and perform an operation. in which case you need to use get
:
t_1 <- data.frame(x=1:10, y = 11:20)
t <- "t_1"
sum(get(t[1])$x)
#[1] 55
As @joran suggests, the better way to do this is to collect all of the t_n
objects into a list and then operate over the list.
t_list <- mget(t)
lapply(t_list, function(df) sum(df$x))
2
Rather than encourage this somewhat problematic code pattern, you could recommendmget
as a way to collect all thoset_*
objects into a named list.
– joran
Nov 20 '18 at 23:06
add a comment |
I assume that t_1
to t_n
are the names of objects in your environment and that you want to loop over said objects and perform an operation. in which case you need to use get
:
t_1 <- data.frame(x=1:10, y = 11:20)
t <- "t_1"
sum(get(t[1])$x)
#[1] 55
As @joran suggests, the better way to do this is to collect all of the t_n
objects into a list and then operate over the list.
t_list <- mget(t)
lapply(t_list, function(df) sum(df$x))
2
Rather than encourage this somewhat problematic code pattern, you could recommendmget
as a way to collect all thoset_*
objects into a named list.
– joran
Nov 20 '18 at 23:06
add a comment |
I assume that t_1
to t_n
are the names of objects in your environment and that you want to loop over said objects and perform an operation. in which case you need to use get
:
t_1 <- data.frame(x=1:10, y = 11:20)
t <- "t_1"
sum(get(t[1])$x)
#[1] 55
As @joran suggests, the better way to do this is to collect all of the t_n
objects into a list and then operate over the list.
t_list <- mget(t)
lapply(t_list, function(df) sum(df$x))
I assume that t_1
to t_n
are the names of objects in your environment and that you want to loop over said objects and perform an operation. in which case you need to use get
:
t_1 <- data.frame(x=1:10, y = 11:20)
t <- "t_1"
sum(get(t[1])$x)
#[1] 55
As @joran suggests, the better way to do this is to collect all of the t_n
objects into a list and then operate over the list.
t_list <- mget(t)
lapply(t_list, function(df) sum(df$x))
edited Nov 20 '18 at 23:09
answered Nov 20 '18 at 23:04
emilliman5emilliman5
4,00321529
4,00321529
2
Rather than encourage this somewhat problematic code pattern, you could recommendmget
as a way to collect all thoset_*
objects into a named list.
– joran
Nov 20 '18 at 23:06
add a comment |
2
Rather than encourage this somewhat problematic code pattern, you could recommendmget
as a way to collect all thoset_*
objects into a named list.
– joran
Nov 20 '18 at 23:06
2
2
Rather than encourage this somewhat problematic code pattern, you could recommend
mget
as a way to collect all those t_*
objects into a named list.– joran
Nov 20 '18 at 23:06
Rather than encourage this somewhat problematic code pattern, you could recommend
mget
as a way to collect all those t_*
objects into a named list.– joran
Nov 20 '18 at 23:06
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%2f53402771%2fhow-can-i-avoid-operator-is-invalid-for-atomic-vectors%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
mtcars$cyl[1]
is a valid reference,mtcars[1]$cyl
is not– Mako212
Nov 20 '18 at 22:53