How to use variables whose names are with number at end in R loop

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use variables whose names are with number at end in R loop

DavidMarino
Hi all:

Assume that I have variables, say v1, v2,...,v100 and I want to use one
variable in each roop. How can I do this? See below

for (i in 1:100){
f(vi)
}


Thanks

David

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to use variables whose names are with number at end in R loop

arun kirshna
Hi,

If the variables described are the columns in a data.frame,

  set.seed(24)
 dat1 <- as.data.frame(matrix(sample(100,100*10,replace=TRUE),ncol=100))
 f1 <- function(x) mean(x,na.rm=TRUE)
 sapply(1:100,function(i) f1(dat1[,i]))
#or
 sapply(colnames(dat1),function(x) f1(dat1[,x]))

#IF these are standalone vectors
V1 <- dat1[,1]
 V2 <- dat1[,2]
sapply(1:2,function(i) f1(get(paste0("V",i))))

A.K.



On Saturday, December 14, 2013 4:07 PM, Marino David <[hidden email]> wrote:
Hi all:

Assume that I have variables, say v1, v2,...,v100 and I want to use one
variable in each roop. How can I do this? See below

for (i in 1:100){
f(vi)
}


Thanks

David

    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to use variables whose names are with number at end in R loop

Hervé Pagès
In reply to this post by DavidMarino
Hi David,

On 12/14/2013 01:06 PM, Marino David wrote:
> Hi all:
>
> Assume that I have variables, say v1, v2,...,v100 and I want to use one
> variable in each roop. How can I do this? See below
>
> for (i in 1:100){
> f(vi)
> }

for (i in 1:100){
   f(get(paste0("v", i)))
}

Cheers,
H.

>
>
> Thanks
>
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to use variables whose names are with number at end in R loop

DavidMarino
Thanks Hervé Pagès and A. K.. It works.

Thank you!

David


2013/12/14 Hervé Pagès <[hidden email]>

> Hi David,
>
>
> On 12/14/2013 01:06 PM, Marino David wrote:
>
>> Hi all:
>>
>> Assume that I have variables, say v1, v2,...,v100 and I want to use one
>> variable in each roop. How can I do this? See below
>>
>> for (i in 1:100){
>> f(vi)
>> }
>>
>
> for (i in 1:100){
>   f(get(paste0("v", i)))
> }
>
> Cheers,
> H.
>
>
>
>>
>> Thanks
>>
>> David
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/
>> posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: [hidden email]
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.