How to apply a mathematical function to a list of objects in R?
up vote
-2
down vote
favorite
So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.
Here's a small example.
a = 680
b = 820
c = 1040
list = as.list(ls())
fun = function(x){x*.8}
for (i in list){
fun(i)
}
And I get an error saying non-numeric argument to binary operator
. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.
Thank you!!
r list function
add a comment |
up vote
-2
down vote
favorite
So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.
Here's a small example.
a = 680
b = 820
c = 1040
list = as.list(ls())
fun = function(x){x*.8}
for (i in list){
fun(i)
}
And I get an error saying non-numeric argument to binary operator
. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.
Thank you!!
r list function
2
change your list tolist = mget(ls())
instead ofas.list
usemget
then you can be able to do all the other operations
– Onyambu
Nov 19 at 18:25
This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.
Here's a small example.
a = 680
b = 820
c = 1040
list = as.list(ls())
fun = function(x){x*.8}
for (i in list){
fun(i)
}
And I get an error saying non-numeric argument to binary operator
. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.
Thank you!!
r list function
So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.
Here's a small example.
a = 680
b = 820
c = 1040
list = as.list(ls())
fun = function(x){x*.8}
for (i in list){
fun(i)
}
And I get an error saying non-numeric argument to binary operator
. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.
Thank you!!
r list function
r list function
asked Nov 19 at 18:05
D. Rodríguez
12
12
2
change your list tolist = mget(ls())
instead ofas.list
usemget
then you can be able to do all the other operations
– Onyambu
Nov 19 at 18:25
This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29
add a comment |
2
change your list tolist = mget(ls())
instead ofas.list
usemget
then you can be able to do all the other operations
– Onyambu
Nov 19 at 18:25
This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29
2
2
change your list to
list = mget(ls())
instead of as.list
use mget
then you can be able to do all the other operations– Onyambu
Nov 19 at 18:25
change your list to
list = mget(ls())
instead of as.list
use mget
then you can be able to do all the other operations– Onyambu
Nov 19 at 18:25
This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29
This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
You are looking for the mget
function: This is a multivariate format of get
thus:
a = 680
b = 820
c = 1040
list = mget(ls())## This is the part you need to change
fun = function(x){x*.8}
for (i in list){
fun(i)
}
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
add a comment |
up vote
1
down vote
You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")
Having said that, this example could be more easily be done using regular vector and scalar multiplication.
x = c(680, 820, 1040)
x * .8
If for some reason you want you use a list and function anyway I would recommend using lapply, like so:
a = 680
b = 820
c = 1040
list = list(a, b, c)
fun = function(x){ x*.8 }
lapply(list, fun)
add a comment |
up vote
1
down vote
Use eapply
to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun)
as variables in an environment can easily be manipulated unintentionally. Refer to the code below:
##### Solution 1 using eapply() #####
# Build a new temporary environment to save all numeric variables
tempEnv <- new.env()
tempEnv$a = 680
tempEnv$b = 820
tempEnv$c = 1040
fun = function(x){x*.8}
# apply fun() to all variables in "tempEnv"
eapply(tempEnv, fun)
##### Solution 2 using lapply() (Recommended) #####
# Define all variables in a list
myList <- list(a = 680,
b = 820,
c = 1040)
# apply fun() to all values in myList
lapply(myList, fun)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are looking for the mget
function: This is a multivariate format of get
thus:
a = 680
b = 820
c = 1040
list = mget(ls())## This is the part you need to change
fun = function(x){x*.8}
for (i in list){
fun(i)
}
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
add a comment |
up vote
0
down vote
accepted
You are looking for the mget
function: This is a multivariate format of get
thus:
a = 680
b = 820
c = 1040
list = mget(ls())## This is the part you need to change
fun = function(x){x*.8}
for (i in list){
fun(i)
}
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are looking for the mget
function: This is a multivariate format of get
thus:
a = 680
b = 820
c = 1040
list = mget(ls())## This is the part you need to change
fun = function(x){x*.8}
for (i in list){
fun(i)
}
You are looking for the mget
function: This is a multivariate format of get
thus:
a = 680
b = 820
c = 1040
list = mget(ls())## This is the part you need to change
fun = function(x){x*.8}
for (i in list){
fun(i)
}
answered Nov 19 at 18:33
Onyambu
15.3k1519
15.3k1519
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
add a comment |
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
– D. Rodríguez
Nov 19 at 18:34
add a comment |
up vote
1
down vote
You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")
Having said that, this example could be more easily be done using regular vector and scalar multiplication.
x = c(680, 820, 1040)
x * .8
If for some reason you want you use a list and function anyway I would recommend using lapply, like so:
a = 680
b = 820
c = 1040
list = list(a, b, c)
fun = function(x){ x*.8 }
lapply(list, fun)
add a comment |
up vote
1
down vote
You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")
Having said that, this example could be more easily be done using regular vector and scalar multiplication.
x = c(680, 820, 1040)
x * .8
If for some reason you want you use a list and function anyway I would recommend using lapply, like so:
a = 680
b = 820
c = 1040
list = list(a, b, c)
fun = function(x){ x*.8 }
lapply(list, fun)
add a comment |
up vote
1
down vote
up vote
1
down vote
You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")
Having said that, this example could be more easily be done using regular vector and scalar multiplication.
x = c(680, 820, 1040)
x * .8
If for some reason you want you use a list and function anyway I would recommend using lapply, like so:
a = 680
b = 820
c = 1040
list = list(a, b, c)
fun = function(x){ x*.8 }
lapply(list, fun)
You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")
Having said that, this example could be more easily be done using regular vector and scalar multiplication.
x = c(680, 820, 1040)
x * .8
If for some reason you want you use a list and function anyway I would recommend using lapply, like so:
a = 680
b = 820
c = 1040
list = list(a, b, c)
fun = function(x){ x*.8 }
lapply(list, fun)
answered Nov 19 at 18:19
vectorson
193
193
add a comment |
add a comment |
up vote
1
down vote
Use eapply
to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun)
as variables in an environment can easily be manipulated unintentionally. Refer to the code below:
##### Solution 1 using eapply() #####
# Build a new temporary environment to save all numeric variables
tempEnv <- new.env()
tempEnv$a = 680
tempEnv$b = 820
tempEnv$c = 1040
fun = function(x){x*.8}
# apply fun() to all variables in "tempEnv"
eapply(tempEnv, fun)
##### Solution 2 using lapply() (Recommended) #####
# Define all variables in a list
myList <- list(a = 680,
b = 820,
c = 1040)
# apply fun() to all values in myList
lapply(myList, fun)
add a comment |
up vote
1
down vote
Use eapply
to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun)
as variables in an environment can easily be manipulated unintentionally. Refer to the code below:
##### Solution 1 using eapply() #####
# Build a new temporary environment to save all numeric variables
tempEnv <- new.env()
tempEnv$a = 680
tempEnv$b = 820
tempEnv$c = 1040
fun = function(x){x*.8}
# apply fun() to all variables in "tempEnv"
eapply(tempEnv, fun)
##### Solution 2 using lapply() (Recommended) #####
# Define all variables in a list
myList <- list(a = 680,
b = 820,
c = 1040)
# apply fun() to all values in myList
lapply(myList, fun)
add a comment |
up vote
1
down vote
up vote
1
down vote
Use eapply
to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun)
as variables in an environment can easily be manipulated unintentionally. Refer to the code below:
##### Solution 1 using eapply() #####
# Build a new temporary environment to save all numeric variables
tempEnv <- new.env()
tempEnv$a = 680
tempEnv$b = 820
tempEnv$c = 1040
fun = function(x){x*.8}
# apply fun() to all variables in "tempEnv"
eapply(tempEnv, fun)
##### Solution 2 using lapply() (Recommended) #####
# Define all variables in a list
myList <- list(a = 680,
b = 820,
c = 1040)
# apply fun() to all values in myList
lapply(myList, fun)
Use eapply
to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun)
as variables in an environment can easily be manipulated unintentionally. Refer to the code below:
##### Solution 1 using eapply() #####
# Build a new temporary environment to save all numeric variables
tempEnv <- new.env()
tempEnv$a = 680
tempEnv$b = 820
tempEnv$c = 1040
fun = function(x){x*.8}
# apply fun() to all variables in "tempEnv"
eapply(tempEnv, fun)
##### Solution 2 using lapply() (Recommended) #####
# Define all variables in a list
myList <- list(a = 680,
b = 820,
c = 1040)
# apply fun() to all values in myList
lapply(myList, fun)
edited Nov 19 at 22:13
answered Nov 19 at 18:19
Jason Jisu Park
1266
1266
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53380325%2fhow-to-apply-a-mathematical-function-to-a-list-of-objects-in-r%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
change your list to
list = mget(ls())
instead ofas.list
usemget
then you can be able to do all the other operations– Onyambu
Nov 19 at 18:25
This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29