[R] Converting a string to variable names

Jim Lemon drjimlemon at gmail.com
Tue Nov 14 23:39:10 CET 2017


Hi Ruiyang,
I think you want "get":

For (index in seq(1,16)){
plot(x=(a given set of value),y=get(paste(“PC”,as.character(index),sep=“”)))
}

On Wed, Nov 15, 2017 at 7:43 AM, 刘瑞阳 <ruiyangliu94 at gmail.com> wrote:
> Hi,
> Suppose that I want to do a series of plots with the y value for each plot as PC1, PC2, PC3… How could I accomplish this using a for loop?
> Suppose the code like this:
>
> For (index in seq(1,16)){
> plot(x=(a given set of value),y=paste(“PC”,as.character(index),sep=“”)
> }
>
> But this would not work because y is assigned a string instead of the variable names. So how could I assign y with a variable name instead of a string? Your reply would be appreciated!
> Ruiyang Liu
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list