[R] simple problem with unquoting argument
Ivan Krylov
|kry|ov @end|ng |rom d|@root@org
Wed Jul 3 10:25:02 CEST 2024
В Wed, 3 Jul 2024 10:13:59 +0200
Troels Ring <tring using gvdnet.dk> пишет:
> Now e looks right - but I have been unable to find out how to get the
> string e converted to the proper argument for sum() - i.e. what is
> function xx?
get(e) will return the value of the variable with the name stored in
the variable e.
A more idiomatic variant will require more changes:
1. Create the "adds" variable as a list, so that it could contain other
arbitrary R values:
adds <- list()
2. Instead of assigning adds1 <- something(), adds2 <-
something_else(), ..., assign to the elements of the list:
adds[[1]] <- something()
adds[[2]] <- something_else()
...
3. Now you can use the same syntax to access the elements of the list:
SS[i] <- sum(adds[[i]])
As a bonus, you can use the "apply" family of R functions that will
perform the loop for you: instead of SS <- c(); for (i in 1:11) SS[i]
<- sum(adds[[i]]) you can write
SS <- vapply(adds, sum, numeric(1))
...and it will perform the same loop inside it, verifying each time
that sum(adds[[i]]) returns a single number.
--
Best regards,
Ivan
P.S. I'm sorry for letting our project lapse.
More information about the R-help
mailing list