Skip to content
Back to formatted view

Raw Message

Message-ID: <5BF29D57-F9AC-42E1-93D1-628F67DF3859@comcast.net>
Date: 2011-08-18T06:37:21Z
From: David Winsemius
Subject: can i use a variable to describe the dimension name of a data.frame?
In-Reply-To: <CAMUSh4r+RA6qfTy+0J1ncKz4O47H4SKWNihbPxH8zaq8Hq=a_Q@mail.gmail.com>

On Aug 18, 2011, at 1:36 AM, Jie TANG wrote:

> for example:
>  a data.frame
>  data
>  V1  V2 V3
>  1     2   3
>  4     5   6
>
> If I want to calculate the V2
> I can get data$V2
> But now I want to calculate the dimension name and then analysis the  
> data.
> for example
> No=3*x ;x=1,2,3,...n
> anadata=data$VNo
> How could I do?

If you are asking how to access the n-th column then try this:

data[[n]]  # returns a vector

  or

data[1:5]  # returns a dataframe

You could also use:

vname <- paste("V", 1:5, sep="")
data[vname]

This does suggest that yoy need to read your introductory material  
more closely, because this would be covered in an early chapter.

-- 

David Winsemius, MD
West Hartford, CT