Working with lists with numerical names

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

Working with lists with numerical names

Christopher Swingley
Greetings!

I'm have a hard time working with some data I imported from a baseball
database.  Several of the database columns have numbers in them (2B,
3B), and when I try to use these vectors from the data frame, I get
syntax errors, probably because it's interpreting the name as a number:

 > show(batting2005)
   playerID yearID stint teamID lgID   G  AB   R   H 2B 3B HR RBI SB CS  BB
   1   robleos01   2005     1    LAN   NL 110 364  44  99 18  1  5 34  0  8  31
   2   iguchta01   2005     1    CHA   AL 135 511  74 142 25  6 15 71 15  5  47
   3   molinya01   2005     1    SLN   NL 114 385  36  97 15  1  8 49  2  3  23
   . . .
 > print(batting2005$HR)
   [1]  5 15  8  3 14  3  6 21  8  7  9 27 12  5 14  8 28  9 22 15  5
   22  9 10  1
   . . .
 > print(batting2005$2B)
 Error: syntax error in "print(batting2005$2"

 > SLG<-(H + 2B + 3B * 2 + HR * 3) / AB;
 Error: syntax error in "SLG<-(H + 2B"      # batting2005 is attached

 > SLG<-(H + "2B" + "3B" * 2 + HR * 3) / AB;
 Error in H + "2B" : non-numeric argument to binary operator

Is there a way to "escape" the '2B' somehow or encapsulate it so that R
knows I'm talking about that particular numeric vector?

Thanks,

Chris
--
Christopher S. Swingley          email: [hidden email]
Intl. Arctic Research Center
University of Alaska Fairbanks   www.frontier.iarc.uaf.edu/~cswingle/

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

Re: Working with lists with numerical names

Sundar Dorai-Raj


Christopher Swingley wrote:

> Greetings!
>
> I'm have a hard time working with some data I imported from a baseball
> database.  Several of the database columns have numbers in them (2B,
> 3B), and when I try to use these vectors from the data frame, I get
> syntax errors, probably because it's interpreting the name as a number:
>
>  > show(batting2005)
>    playerID yearID stint teamID lgID   G  AB   R   H 2B 3B HR RBI SB CS  BB
>    1   robleos01   2005     1    LAN   NL 110 364  44  99 18  1  5 34  0  8  31
>    2   iguchta01   2005     1    CHA   AL 135 511  74 142 25  6 15 71 15  5  47
>    3   molinya01   2005     1    SLN   NL 114 385  36  97 15  1  8 49  2  3  23
>    . . .
>  > print(batting2005$HR)
>    [1]  5 15  8  3 14  3  6 21  8  7  9 27 12  5 14  8 28  9 22 15  5
>    22  9 10  1
>    . . .
>  > print(batting2005$2B)
>  Error: syntax error in "print(batting2005$2"
>
>  > SLG<-(H + 2B + 3B * 2 + HR * 3) / AB;
>  Error: syntax error in "SLG<-(H + 2B"      # batting2005 is attached
>
>  > SLG<-(H + "2B" + "3B" * 2 + HR * 3) / AB;
>  Error in H + "2B" : non-numeric argument to binary operator
>
> Is there a way to "escape" the '2B' somehow or encapsulate it so that R
> knows I'm talking about that particular numeric vector?
>
> Thanks,
>
> Chris

Hi, Chris,

 > x <- data.frame(1:5, 6:10)
 > names(x) <- c("R", "2B")
 > x
   R 2B
1 1  6
2 2  7
3 3  8
4 4  9
5 5 10
 > x$"2B"
[1]  6  7  8  9 10
 > with(x, R + `2B`)
[1]  7  9 11 13 15

HTH,

--sundar

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

Re: Working with lists with numerical names

jholtman
In reply to this post by Christopher Swingley
> x
   playerID yearID stint teamID lgID   G  AB  R   H 2B 3B HR RBI SB CS BB
1 robleos01   2005     1    LAN   NL 110 364 44  99 18  1  5  34  0  8 31
2 iguchta01   2005     1    CHA   AL 135 511 74 142 25  6 15  71 15  5 47
3 molinya01   2005     1    SLN   NL 114 385 36  97 15  1  8  49  2  3 23
> x[["H]]

+ > x[["H"]]
[1]  99 142  97
> x[["2B"]]
[1] 18 25 15
>

On 2/23/06, Christopher Swingley <[hidden email]> wrote:

>
> Greetings!
>
> I'm have a hard time working with some data I imported from a baseball
> database.  Several of the database columns have numbers in them (2B,
> 3B), and when I try to use these vectors from the data frame, I get
> syntax errors, probably because it's interpreting the name as a number:
>
> > show(batting2005)
>   playerID yearID stint teamID lgID   G  AB   R   H 2B 3B HR RBI SB CS  BB
>
>   1   robleos01   2005     1    LAN   NL 110 364  44  99 18  1  5
> 34  0  8  31
>   2   iguchta01   2005     1    CHA   AL 135 511  74 142 25  6 15 71
> 15  5  47
>   3   molinya01   2005     1    SLN   NL 114 385  36  97 15  1  8
> 49  2  3  23
>   . . .
> > print(batting2005$HR)
>   [1]  5 15  8  3 14  3  6 21  8  7  9 27 12  5 14  8 28  9 22 15  5
>   22  9 10  1
>   . . .
> > print(batting2005$2B)
> Error: syntax error in "print(batting2005$2"
>
> > SLG<-(H + 2B + 3B * 2 + HR * 3) / AB;
> Error: syntax error in "SLG<-(H + 2B"      # batting2005 is attached
>
> > SLG<-(H + "2B" + "3B" * 2 + HR * 3) / AB;
> Error in H + "2B" : non-numeric argument to binary operator
>
> Is there a way to "escape" the '2B' somehow or encapsulate it so that R
> knows I'm talking about that particular numeric vector?
>
> Thanks,
>
> Chris
> --
> Christopher S. Swingley          email: [hidden email]
> Intl. Arctic Research Center
> University of Alaska Fairbanks   www.frontier.iarc.uaf.edu/~cswingle/
>
> ______________________________________________
> [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<http://www.r-project.org/posting-guide.html>
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What the problem you are trying to solve?

        [[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
Reply | Threaded
Open this post in threaded view
|

Re: Working with lists with numerical names

Christopher Swingley
In reply to this post by Sundar Dorai-Raj
Sundar,

* Sundar Dorai-Raj <[hidden email]> [2006-Feb-23 14:23 AKST]:

> Hi, Chris,
>
> > x <- data.frame(1:5, 6:10)
> > names(x) <- c("R", "2B")
> > x
>   R 2B
> 1 1  6
> 2 2  7
> 3 3  8
> 4 4  9
> 5 5 10
> > x$"2B"

Thanks.  I swear I tried just about every other combination of possible
escape character.  But I didn't try:

    frame$"vector"

or

    `vector`

My bad.

I *knew* it was something simple!

Cheers,

Chris
--
Christopher S. Swingley          email: [hidden email]
Intl. Arctic Research Center
University of Alaska Fairbanks   www.frontier.iarc.uaf.edu/~cswingle/

______________________________________________
[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