Converting a string to variable names

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting a string to variable names

刘瑞阳
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
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting a string to variable names

Jim Lemon-4
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, 刘瑞阳 <[hidden email]> 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
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting a string to variable names

刘瑞阳
Hi,

Thanks for your reply.

I just came up with another question about a similar but different thing:

Say I have a bunch of data.frame variables named P1,P2,P3… I want to assign them row names according to symbols in the first column, and I want to do this using a for loop. How could I accomplish this?


for (test_sample in c(1:10)){
+ x<-as.name(paste(“P",as.character(test_sampel),sep=""))
+ rownames(x)<-get((paste(“P”,as.character(test_sample),sep="")))[,1]
+ }

This would not work probably because x is simply a name instead of a data.frame variable(Error: "attempt to set 'rownames' on an object with no dimensions"). But I could not find the right way out… How should I solve it?

Thanks!

Ruiyang


> On Nov 14, 2017, at 4:39 PM, Jim Lemon <[hidden email]> wrote:
>
> 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, 刘瑞阳 <[hidden email]> 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
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting a string to variable names

Jim Lemon-4
Hi Ruiyang,
In this case you don't want the "get", just the strings produced by "paste":

mydf<-data.frame(col1=LETTERS[1:10],col2=1:10)
rownames(mydf)<-paste("P",mydf$col1,sep="")

Jim

On Thu, Nov 16, 2017 at 9:22 AM, 刘瑞阳 <[hidden email]> wrote:

> Hi,
>
> Thanks for your reply.
>
> I just came up with another question about a similar but different thing:
>
> Say I have a bunch of data.frame variables named P1,P2,P3… I want to assign them row names according to symbols in the first column, and I want to do this using a for loop. How could I accomplish this?
>
>
> for (test_sample in c(1:10)){
> + x<-as.name(paste(“P",as.character(test_sampel),sep=""))
> + rownames(x)<-get((paste(“P”,as.character(test_sample),sep="")))[,1]
> + }
>
> This would not work probably because x is simply a name instead of a data.frame variable(Error: "attempt to set 'rownames' on an object with no dimensions"). But I could not find the right way out… How should I solve it?
>
> Thanks!
>
> Ruiyang
>
>
>> On Nov 14, 2017, at 4:39 PM, Jim Lemon <[hidden email]> wrote:
>>
>> 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, 刘瑞阳 <[hidden email]> 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
>>> ______________________________________________
>>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting a string to variable names

MacQueen, Don
In reply to this post by 刘瑞阳
Do you mean that you have
    One data frame, and it has a bunch of variables within it named P1, P2, P3...
or
    A bunch of data frames names P1, P2, P3...
?

I'll assume it's the latter. Here is one way:

dfnms < c('P1', 'P2', 'P3')

for (nm in dfnms) {
  tmp <- get(nm)
  rownames(tmp) <- tmp[[1]]
  assign(nm, tmp)
}

The get() and assign() functions have position and/or environment arguments. You might have to supply those.

You wouldn't have to use get() and assign() if the data frames were stored as elements of a list. This is sometimes a good way to do things.

mydfs <- list(P1=P1, P2=P2, P3=P3)
for (idf in seq(mydfs)) rownames(mydfs[[idf]]) <- mydfs[[idf]][[1]]

Of course, then you can't do, for example,
  plot( P1$x, P1$y)
you would have to do
  plot(mydf$P1$x, mydf$P1$y)

or instead of
   with(P1, plot(x,y))
you would have to do
   with(mydf$P1, plot(x,y))

(all of the above is untested, so watch out for typing errors)

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 11/15/17, 2:22 PM, "R-help on behalf of 刘瑞阳" <[hidden email] on behalf of [hidden email]> wrote:

    Hi,
   
    Thanks for your reply.
   
    I just came up with another question about a similar but different thing:
   
    Say I have a bunch of data.frame variables named P1,P2,P3… I want to assign them row names according to symbols in the first column, and I want to do this using a for loop. How could I accomplish this?
   
   
    for (test_sample in c(1:10)){
    + x<-as.name(paste(“P",as.character(test_sampel),sep=""))
    + rownames(x)<-get((paste(“P”,as.character(test_sample),sep="")))[,1]
    + }
   
    This would not work probably because x is simply a name instead of a data.frame variable(Error: "attempt to set 'rownames' on an object with no dimensions"). But I could not find the right way out… How should I solve it?
   
    Thanks!
   
    Ruiyang
   
   
    > On Nov 14, 2017, at 4:39 PM, Jim Lemon <[hidden email]> wrote:
    >
    > 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, 刘瑞阳 <[hidden email]> 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
    >> ______________________________________________
    >> [hidden email] 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.
   
    ______________________________________________
    [hidden email] 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.

______________________________________________
[hidden email] 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.