Extracting a column name in loop?

View: New views
4 Messages — Rating Filter:   Alert me  

Extracting a column name in loop?

by mister_bluesman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am writing a script that will address columns using syntax like:

data_set[,1]

to extract the data from the first column of my data set, for example. This code will be placed in a loop (where the column reference will be placed by a variable).

What I also need to do is extract the column NAME for a given column being processed in the loop. The dataframe has been set so that R knows that the top line refers to column headers.

Can anyone help me understand how to do this?

Thanks.

Re: Extracting a column name in loop?

by Mark Knecht :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 8, 2009 at 8:41 AM,
mister_bluesman<mister_bluesman@...> wrote:

>
> Hi,
>
> I am writing a script that will address columns using syntax like:
>
> data_set[,1]
>
> to extract the data from the first column of my data set, for example. This
> code will be placed in a loop (where the column reference will be placed by
> a variable).
>
> What I also need to do is extract the column NAME for a given column being
> processed in the loop. The dataframe has been set so that R knows that the
> top line refers to column headers.
>
> Can anyone help me understand how to do this?
>
> Thanks.

Possibly something like

names(data_set)[i]

?

HTH,
Mark

______________________________________________
R-help@... mailing list
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.

Parent Message unknown Re: Extracting a column name in loop?

by Moshe Olshansky-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


If df is your dataframe then names(df) contains the column names and so names(df)[i] is the name of i-th column.

--- On Thu, 9/7/09, mister_bluesman <mister_bluesman@...> wrote:

> From: mister_bluesman <mister_bluesman@...>
> Subject: [R]  Extracting a column name in loop?
> To: r-help@...
> Received: Thursday, 9 July, 2009, 1:41 AM
>
> Hi,
>
> I am writing a script that will address columns using
> syntax like:
>
> data_set[,1]
>
> to extract the data from the first column of my data set,
> for example. This
> code will be placed in a loop (where the column reference
> will be placed by
> a variable).
>
> What I also need to do is extract the column NAME for a
> given column being
> processed in the loop. The dataframe has been set so that R
> knows that the
> top line refers to column headers.
>
> Can anyone help me understand how to do this?
>
> Thanks.
> --
> View this message in context: http://www.nabble.com/Extracting-a-column-name-in-loop--tp24393160p24393160.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@...
> mailing list
> 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.
>

______________________________________________
R-help@... mailing list
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.

Re: Extracting a column name in loop?

by mister_bluesman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you both for that. Much appreciated.

mister_bluesman wrote:
Hi,

I am writing a script that will address columns using syntax like:

data_set[,1]

to extract the data from the first column of my data set, for example. This code will be placed in a loop (where the column reference will be placed by a variable).

What I also need to do is extract the column NAME for a given column being processed in the loop. The dataframe has been set so that R knows that the top line refers to column headers.

Can anyone help me understand how to do this?

Thanks.