Finding the position of a variable in a data.frame

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

Finding the position of a variable in a data.frame

John Kane-2
Simple problem but I don't see the answer. I'm trying
to clean up some data
I have 120 columns in a data.frame.  I have one value
in a column named "blaw" that I want to change. How do
I find the coordinates. I can find the row by doing a
subset on the data.frame but how do I find out here
"blaw " is in columns without manually counting them
or converting names(Df) to a list and reading down the
list.

Simple example

cat <- c( 3,5,6,8,0)
dog <- c(3,5,3,6, 0)
rat <- c (5, 5, 4, 9, 0)
bat <- c( 12, 42, 45, 32, 54)

Df <- data.frame(cbind(cat, dog, rat, bat))
Df
subset(Df, bat >= 50)

----results
  cat dog rat bat
5   0   0   0  54


Thus I know that my target is in row 5 but how do I
figure out where 'bat' is?  

All I want to do is be able to say
Df[5,4] <- 100

Is there some way to have function(bat) return the
column number: some kind of a colnum() function?  I
had thought that I had found somthing  in
library(gdata) matchcols but no luck.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

jholtman
?which

> which(Df >= 50, arr.ind=T)
  row col
5   5   4



On 8/2/06, John Kane <[hidden email]> wrote:

>
> Simple problem but I don't see the answer. I'm trying
> to clean up some data
> I have 120 columns in a data.frame.  I have one value
> in a column named "blaw" that I want to change. How do
> I find the coordinates. I can find the row by doing a
> subset on the data.frame but how do I find out here
> "blaw " is in columns without manually counting them
> or converting names(Df) to a list and reading down the
> list.
>
> Simple example
>
> cat <- c( 3,5,6,8,0)
> dog <- c(3,5,3,6, 0)
> rat <- c (5, 5, 4, 9, 0)
> bat <- c( 12, 42, 45, 32, 54)
>
> Df <- data.frame(cbind(cat, dog, rat, bat))
> Df
> subset(Df, bat >= 50)
>
> ----results
> cat dog rat bat
> 5   0   0   0  54
>
>
> Thus I know that my target is in row 5 but how do I
> figure out where 'bat' is?
>
> All I want to do is be able to say
> Df[5,4] <- 100
>
> Is there some way to have function(bat) return the
> column number: some kind of a colnum() function?  I
> had thought that I had found somthing  in
> library(gdata) matchcols but no luck.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>



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

What is 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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

Chuck Cleland
In reply to this post by John Kane-2
John Kane wrote:

> Simple problem but I don't see the answer. I'm trying
> to clean up some data
> I have 120 columns in a data.frame.  I have one value
> in a column named "blaw" that I want to change. How do
> I find the coordinates. I can find the row by doing a
> subset on the data.frame but how do I find out here
> "blaw " is in columns without manually counting them
> or converting names(Df) to a list and reading down the
> list.
>
> Simple example
>
> cat <- c( 3,5,6,8,0)
> dog <- c(3,5,3,6, 0)
> rat <- c (5, 5, 4, 9, 0)
> bat <- c( 12, 42, 45, 32, 54)
>
> Df <- data.frame(cbind(cat, dog, rat, bat))
> Df
> subset(Df, bat >= 50)
>
> ----results
>   cat dog rat bat
> 5   0   0   0  54
>
>
> Thus I know that my target is in row 5 but how do I
> figure out where 'bat' is?  

grep("bat", names(Df))

> All I want to do is be able to say
> Df[5,4] <- 100

Why not do it this way?

Df$bat <- replace(Df$bat, Df$bat >=50, 100)

> Is there some way to have function(bat) return the
> column number: some kind of a colnum() function?  I
> had thought that I had found somthing  in
> library(gdata) matchcols but no luck.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

RKoenker
In reply to this post by John Kane-2
it is the well-known wicked which problem:  if you had (grammatically  
incorrectly)
thought "... which I want to change" then you might have been led
to type (in another window):

        ?which

and you would have seen the light.  Maybe that() should be an alias
for which()?

url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820


On Aug 2, 2006, at 4:01 PM, John Kane wrote:

> Simple problem but I don't see the answer. I'm trying
> to clean up some data
> I have 120 columns in a data.frame.  I have one value
> in a column named "blaw" that I want to change. How do
> I find the coordinates. I can find the row by doing a
> subset on the data.frame but how do I find out here
> "blaw " is in columns without manually counting them
> or converting names(Df) to a list and reading down the
> list.
>
> Simple example
>
> cat <- c( 3,5,6,8,0)
> dog <- c(3,5,3,6, 0)
> rat <- c (5, 5, 4, 9, 0)
> bat <- c( 12, 42, 45, 32, 54)
>
> Df <- data.frame(cbind(cat, dog, rat, bat))
> Df
> subset(Df, bat >= 50)
>
> ----results
>   cat dog rat bat
> 5   0   0   0  54
>
>
> Thus I know that my target is in row 5 but how do I
> figure out where 'bat' is?
>
> All I want to do is be able to say
> Df[5,4] <- 100
>
> Is there some way to have function(bat) return the
> column number: some kind of a colnum() function?  I
> had thought that I had found somthing  in
> library(gdata) matchcols but no luck.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

John Kane-2
In reply to this post by jholtman

--- jim holtman <[hidden email]> wrote:

> ?which
>
> > which(Df >= 50, arr.ind=T)
>   row col
> 5   5   4



I knew it was going to be blinding obvious! I even
read
?which somehow misunderstood arr.ind.

Thanks again.

>
> On 8/2/06, John Kane <[hidden email]> wrote:
> >
> > Simple problem but I don't see the answer. I'm
> trying
> > to clean up some data
> > I have 120 columns in a data.frame.  I have one
> value
> > in a column named "blaw" that I want to change.
> How do
> > I find the coordinates. I can find the row by
> doing a
> > subset on the data.frame but how do I find out
> here
> > "blaw " is in columns without manually counting
> them
> > or converting names(Df) to a list and reading down
> the
> > list.
> >
> > Simple example
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 0)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > Df
> > subset(Df, bat >= 50)
> >
> > ----results
> > cat dog rat bat
> > 5   0   0   0  54
> >
> >
> > Thus I know that my target is in row 5 but how do
> I
> > figure out where 'bat' is?
> >
> > All I want to do is be able to say
> > Df[5,4] <- 100
> >
> > Is there some way to have function(bat) return the
> > column number: some kind of a colnum() function?
> I
> > had thought that I had found somthing  in
> > library(gdata) matchcols but no luck.
> >
> > ______________________________________________
> > [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
> > and provide commented, minimal, self-contained,
> reproducible code.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

John Kane-2
In reply to this post by Chuck Cleland

--- Chuck Cleland <[hidden email]> wrote:

> John Kane wrote:
> > Simple problem but I don't see the answer. I'm
> trying
> > to clean up some data
> > I have 120 columns in a data.frame.  I have one
> value
> > in a column named "blaw" that I want to change.
> How do
> > I find the coordinates. I can find the row by
> doing a
> > subset on the data.frame but how do I find out
> here
> > "blaw " is in columns without manually counting
> them
> > or converting names(Df) to a list and reading down
> the
> > list.
> >
> > Simple example
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 0)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > Df
> > subset(Df, bat >= 50)
> >
> > ----results
> >   cat dog rat bat
> > 5   0   0   0  54
> >
> >
> > Thus I know that my target is in row 5 but how do
> I
> > figure out where 'bat' is?  
>
> grep("bat", names(Df))

Thank you, I have never used grep.  More reading :(

>
> > All I want to do is be able to say
> > Df[5,4] <- 100
>
> Why not do it this way?
>
> Df$bat <- replace(Df$bat, Df$bat >=50, 100)

Maybe because it is too blinding simple?  I'm still
thinking in SAS or Systat.

A great solution

thanks very much

>
> > Is there some way to have function(bat) return the
> > column number: some kind of a colnum() function?
> I
> > had thought that I had found somthing  in
> > library(gdata) matchcols but no luck.
> >
> > ______________________________________________
> > [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
> > and provide commented, minimal, self-contained,
> reproducible code.
>
> --
> Chuck Cleland, Ph.D.
> NDRI, Inc.
> 71 West 23rd Street, 8th floor
> New York, NY 10010
> tel: (212) 845-4495 (Tu, Th)
> tel: (732) 512-0171 (M, W, F)
> fax: (917) 438-0894
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

John Kane-2
In reply to this post by RKoenker

--- roger koenker <[hidden email]> wrote:

> it is the well-known wicked which problem:  if you
> had (grammatically  
> incorrectly)
> thought "... which I want to change" then you might
> have been led
> to type (in another window):
>
> ?which
>
> and you would have seen the light.  Maybe that()
> should be an alias
> for which()?

One also has to understand which and I missed it.  I
don't think that which is really that grammatically
incorrect anymore.

Thanks

>
> url:    www.econ.uiuc.edu/~roger            Roger
> Koenker
> email    [hidden email]            Department of
> Economics
> vox:     217-333-4558                University of
> Illinois
> fax:       217-244-6678                Champaign, IL
> 61820
>
>
> On Aug 2, 2006, at 4:01 PM, John Kane wrote:
>
> > Simple problem but I don't see the answer. I'm
> trying
> > to clean up some data
> > I have 120 columns in a data.frame.  I have one
> value
> > in a column named "blaw" that I want to change.
> How do
> > I find the coordinates. I can find the row by
> doing a
> > subset on the data.frame but how do I find out
> here
> > "blaw " is in columns without manually counting
> them
> > or converting names(Df) to a list and reading down
> the
> > list.
> >
> > Simple example
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 0)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > Df
> > subset(Df, bat >= 50)
> >
> > ----results
> >   cat dog rat bat
> > 5   0   0   0  54
> >
> >
> > Thus I know that my target is in row 5 but how do
> I
> > figure out where 'bat' is?
> >
> > All I want to do is be able to say
> > Df[5,4] <- 100
> >
> > Is there some way to have function(bat) return the
> > column number: some kind of a colnum() function?
> I
> > had thought that I had found somthing  in
> > library(gdata) matchcols but no luck.
> >
> > ______________________________________________
> > [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
> > and provide commented, minimal, self-contained,
> reproducible code.
>
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

Gerald Jansen
In reply to this post by Chuck Cleland
On Wed, 02 Aug 2006 17:12:53 -0400, Chuck Cleland wrote:
> Why not do it this way?
>
> Df$bat <- replace(Df$bat, Df$bat >=50, 100)

Is that any different, performancewise, than the following?

Df$bat[Df$bat >= 50] <- 100

Gerald Jansen


...
John Kane wrote:
> Simple example
>
> cat <- c( 3,5,6,8,0)
> dog <- c(3,5,3,6, 0)
> rat <- c (5, 5, 4, 9, 0)
> bat <- c( 12, 42, 45, 32, 54)
>
> Df <- data.frame(cbind(cat, dog, rat, bat))
...

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Vector DF [a, b] to replace values :was Finding the position of a variable in a data.frame

John Kane-2
In reply to this post by jholtman

--- jim holtman <[hidden email]> wrote:

> ?which
>
> > which(Df >= 50, arr.ind=T)
>   row col
> 5   5   4

This works very nicely as has some other suggestions
on how to replace a value. Assuming that I have more
than one correction to make where Df >= 50, can I use
vectors in the Df[] to do this.

My attempt shows that I can use vectors but it appears
thatthere is something wrong with my logic

Eample

cat <- c( 3,5,6,8,0)
dog <- c(3,5,3,6, 0)
rat <- c (5, 5, 4, 9, 51)
bat <- c( 12, 42, 45, 32, 54)

Df <- data.frame(cbind(cat, dog, rat, bat))
post <- which(Df >= 50, arr.ind=T)  # find values .=
50
post
correction <- c(77, 88)  # new correct values
row <- post[ ,1]      # vector for row number
col <- post[ ,2]       # vector for column number

Df[row,col] <-  correction
Df

-------result---------
  cat dog rat bat
1   3   3   5  12
2   5   5   5  42
3   6   3   4  45
4   8   6   9  32
5   0   0  88  88

I am replacing both instances with 88 which is not
correct

Thanks



> On 8/2/06, John Kane <[hidden email]> wrote:
> >
> > Simple problem but I don't see the answer. I'm
> trying
> > to clean up some data
> > I have 120 columns in a data.frame.  I have one
> value
> > in a column named "blaw" that I want to change.
> How do
> > I find the coordinates. I can find the row by
> doing a
> > subset on the data.frame but how do I find out
> here
> > "blaw " is in columns without manually counting
> them
> > or converting names(Df) to a list and reading down
> the
> > list.
> >
> > Simple example
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 0)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > Df
> > subset(Df, bat >= 50)
> >
> > ----results
> > cat dog rat bat
> > 5   0   0   0  54
> >
> >
> > Thus I know that my target is in row 5 but how do
> I
> > figure out where 'bat' is?
> >
> > All I want to do is be able to say
> > Df[5,4] <- 100
> >
> > Is there some way to have function(bat) return the
> > column number: some kind of a colnum() function?
> I
> > had thought that I had found somthing  in
> > library(gdata) matchcols but no luck.
> >
> > ______________________________________________
> > [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
> > and provide commented, minimal, self-contained,
> reproducible code.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Vector DF [a, b] to replace values :was Finding the position of a variable in a data.frame

Peter Dalgaard
John Kane <[hidden email]> writes:

> --- jim holtman <[hidden email]> wrote:
>
> > ?which
> >
> > > which(Df >= 50, arr.ind=T)
> >   row col
> > 5   5   4
>
> This works very nicely as has some other suggestions
> on how to replace a value. Assuming that I have more
> than one correction to make where Df >= 50, can I use
> vectors in the Df[] to do this.
>
> My attempt shows that I can use vectors but it appears
> thatthere is something wrong with my logic
>
> Eample
>
> cat <- c( 3,5,6,8,0)
> dog <- c(3,5,3,6, 0)
> rat <- c (5, 5, 4, 9, 51)
> bat <- c( 12, 42, 45, 32, 54)
>
> Df <- data.frame(cbind(cat, dog, rat, bat))
> post <- which(Df >= 50, arr.ind=T)  # find values .=
> 50
> post
> correction <- c(77, 88)  # new correct values
> row <- post[ ,1]      # vector for row number
> col <- post[ ,2]       # vector for column number
>
> Df[row,col] <-  correction
> Df
>
> -------result---------
>   cat dog rat bat
> 1   3   3   5  12
> 2   5   5   5  42
> 3   6   3   4  45
> 4   8   6   9  32
> 5   0   0  88  88
>
> I am replacing both instances with 88 which is not
> correct

You're being bitten by two issues here: One is that Df[row,col] does
not vectorize in parallel in the two indices: Df[1:2,1:2] has *four*
elements, not two. Another is that data frames are not matrices: you
can have different types of values in different columns, so you cannot
expect to pick an arbitrary set of elements and assign a vector into
it (well, it doesn't work, anyway).

This sort of stuff works much easier with matrices, where we also have
the wonderful feature of indexing with a matrix:

> M <- cbind(cat, dog, rat, bat)
> M[post]<- correction
> M
     cat dog rat bat
[1,]   3   3   5  12
[2,]   5   5   5  42
[3,]   6   3   4  45
[4,]   8   6   9  32
[5,]   0   0  77  88


--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Vector DF [a, b] to replace values :was Finding the position of a variable in a data.frame

John Kane-2

--- Peter Dalgaard <[hidden email]> wrote:

> John Kane <[hidden email]> writes:
>
> > --- jim holtman <[hidden email]> wrote:
> >
> > > ?which
> > >
> > > > which(Df >= 50, arr.ind=T)
> > >   row col
> > > 5   5   4
> >
> > This works very nicely as has some other
> suggestions
> > on how to replace a value. Assuming that I have
> more
> > than one correction to make where Df >= 50, can I
> use
> > vectors in the Df[] to do this.
> >
> > My attempt shows that I can use vectors but it
> appears
> > thatthere is something wrong with my logic
> >
> > Eample
> >
> > cat <- c( 3,5,6,8,0)
> > dog <- c(3,5,3,6, 0)
> > rat <- c (5, 5, 4, 9, 51)
> > bat <- c( 12, 42, 45, 32, 54)
> >
> > Df <- data.frame(cbind(cat, dog, rat, bat))
> > post <- which(Df >= 50, arr.ind=T)  # find values
> .=
> > 50
> > post
> > correction <- c(77, 88)  # new correct values
> > row <- post[ ,1]      # vector for row number
> > col <- post[ ,2]       # vector for column number
> >
> > Df[row,col] <-  correction
> > Df
> >
> > -------result---------
> >   cat dog rat bat
> > 1   3   3   5  12
> > 2   5   5   5  42
> > 3   6   3   4  45
> > 4   8   6   9  32
> > 5   0   0  88  88
> >
> > I am replacing both instances with 88 which is not
> > correct
>
> You're being bitten by two issues here: One is that
> Df[row,col] does
> not vectorize in parallel in the two indices:
> Df[1:2,1:2] has *four*
> elements, not two.

It took a moment to see it but it's rather obvious
now. I'm still fuzzy on the differences in behaviour
between a data frame and a matrix.  


>  Another is that data frames are
> not matrices: you
> can have different types of values in different
> columns, so you cannot
> expect to pick an arbitrary set of elements and
> assign a vector into
> it (well, it doesn't work, anyway).
>
> This sort of stuff works much easier with matrices,
> where we also have
> the wonderful feature of indexing with a matrix:
>
> > M <- cbind(cat, dog, rat, bat)
> > M[post]<- correction
> > M
>      cat dog rat bat
> [1,]   3   3   5  12
> [2,]   5   5   5  42
> [3,]   6   3   4  45
> [4,]   8   6   9  32
> [5,]   0   0  77  88

Which of course I, stupidly  thought that I was doing
in the data frame.

Thank you very much.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

Don MacQueen
In reply to this post by John Kane-2
You don't need to find out the column index. This works:

    Df[5,'bat'] <- 100

-Don

At 5:01 PM -0400 8/2/06, John Kane wrote:

>Simple problem but I don't see the answer. I'm trying
>to clean up some data
>I have 120 columns in a data.frame.  I have one value
>in a column named "blaw" that I want to change. How do
>I find the coordinates. I can find the row by doing a
>subset on the data.frame but how do I find out here
>"blaw " is in columns without manually counting them
>or converting names(Df) to a list and reading down the
>list.
>
>Simple example
>
>cat <- c( 3,5,6,8,0)
>dog <- c(3,5,3,6, 0)
>rat <- c (5, 5, 4, 9, 0)
>bat <- c( 12, 42, 45, 32, 54)
>
>Df <- data.frame(cbind(cat, dog, rat, bat))
>Df
>subset(Df, bat >= 50)
>
>----results
>   cat dog rat bat
>5   0   0   0  54
>
>
>Thus I know that my target is in row 5 but how do I
>figure out where 'bat' is?
>
>All I want to do is be able to say
>Df[5,4] <- 100
>
>Is there some way to have function(bat) return the
>column number: some kind of a colnum() function?  I
>had thought that I had found somthing  in
>library(gdata) matchcols but no luck.
>
>______________________________________________
>[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
>and provide commented, minimal, self-contained, reproducible code.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

John Kane-2

--- Don MacQueen <[hidden email]> wrote:

> You don't need to find out the column index. This
> works:
>
>     Df[5,'bat'] <- 100
>
> -Don
>

Thanks, I'd tried
Df[5, bat] <- 100  :(

I never thought of the ' ' being needed.



> At 5:01 PM -0400 8/2/06, John Kane wrote:
> >Simple problem but I don't see the answer. I'm
> trying
> >to clean up some data
> >I have 120 columns in a data.frame.  I have one
> value
> >in a column named "blaw" that I want to change. How
> do
> >I find the coordinates. I can find the row by doing
> a
> >subset on the data.frame but how do I find out here
> >"blaw " is in columns without manually counting
> them
> >or converting names(Df) to a list and reading down
> the
> >list.
> >
> >Simple example
> >
> >cat <- c( 3,5,6,8,0)
> >dog <- c(3,5,3,6, 0)
> >rat <- c (5, 5, 4, 9, 0)
> >bat <- c( 12, 42, 45, 32, 54)
> >
> >Df <- data.frame(cbind(cat, dog, rat, bat))
> >Df
> >subset(Df, bat >= 50)
> >
> >----results
> >   cat dog rat bat
> >5   0   0   0  54
> >
> >
> >Thus I know that my target is in row 5 but how do I
> >figure out where 'bat' is?
> >
> >All I want to do is be able to say
> >Df[5,4] <- 100
> >
> >Is there some way to have function(bat) return the
> >column number: some kind of a colnum() function?  I
> >had thought that I had found somthing  in
> >library(gdata) matchcols but no luck.
> >
> >______________________________________________
> >[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
> >and provide commented, minimal, self-contained,
> reproducible code.
>
>
> --
> --------------------------------------
> Don MacQueen
> Environmental Protection Department
> Lawrence Livermore National Laboratory
> Livermore, CA, USA
> --------------------------------------
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding the position of a variable in a data.frame

Gabor Grothendieck
On 8/3/06, John Kane <[hidden email]> wrote:

>
> --- Don MacQueen <[hidden email]> wrote:
>
> > You don't need to find out the column index. This
> > works:
> >
> >     Df[5,'bat'] <- 100
> >
> > -Don
> >
>
> Thanks, I'd tried
> Df[5, bat] <- 100  :(
>
> I never thought of the ' ' being needed.

Right -- the quotes are not needed if you use $ but they are needed if
you use [.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.