[R] can i use a variable to describe the dimension name of a data.frame?

David Winsemius dwinsemius at comcast.net
Thu Aug 18 08:37:21 CEST 2011


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



More information about the R-help mailing list