[R] simple problem with unquoting argument
Troels Ring
tr|ng @end|ng |rom gvdnet@dk
Wed Jul 3 16:16:23 CEST 2024
Dear Ivan and Rui - thanks a lot for the effective help.
All best wishes Troels
Den 03-07-2024 kl. 10:25 skrev Ivan Krylov:
> В 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.
>
More information about the R-help
mailing list