Skip to content

change value in one cell

5 messages · jour4life, jim holtman, Rolf Turner

#
Hello all,

I am wondering if there is a way to change the value of one cell in R. For
instance let's say I have a hypothetical data frame that looks like this:

Obs X Y Z
1    1 0 1
2    0 0 1
3    1 1 1
4    0 1 1
5    1 1 0
6    0 0 0

I would like to change the value of the 4th observation in the Y column from
1 to 0. It should look like this:

Obs X Y Z
1    1 0 1
2    0 0 1
3    1 1 1
4    0 0 1
5    1 1 0
6    0 0 0

Is it possible to change the value in one command?

Thanks,

Carlos


--
View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589456.html
Sent from the R help mailing list archive at Nabble.com.
#
you probably need to read the Introduction to R to understand indexing:
Obs X Y Z
1   1 1 0 1
2   2 0 0 1
3   3 1 1 1
4   4 0 1 1
5   5 1 1 0
6   6 0 0 0
Obs X Y Z
1   1 1 0 1
2   2 0 0 1
3   3 1 1 1
4   4 0 0 1
5   5 1 1 0
6   6 0 0 0
On Fri, Jun 10, 2011 at 5:42 PM, jour4life <jour4life at gmail.com> wrote:

  
    
#
On 11/06/11 18:14, jour4life wrote:
The error is clear enough, isn't it?  "00 000" is not one of the levels 
of the
"X" column of your data frame; this column is a factor.  Probably because
of the "stringsAsFactors = TRUE" default in options().

Since you appear to have been unaware of the factor nature of "X", 
presumably
you don't really want it to be a factor.  If this is the case execute

     x[,"X"] <- as.character(x[,"X"])

and then your reassignment of the [1,"X"] entry of "x" will work.

If you do want "X" to be a factor you could:

     (a) execute x[,"X"] <- factor(x[,"X"]) *after* doing the 
reassignment, or

     (b) execute levels(x[,"X"]) <- c("00 000",levels(x[,"X"])) *before* 
doing
     the reassignment.

Learn more about how R works.  In particular learn about factors; they are
important and useful.

     cheers,

         Rolf Turner
#
your dataframe has X as a factor; convert to character vector and try again.

x$X <- as.character(x$X)

Sent from my iPad
On Jun 11, 2011, at 2:14, jour4life <jour4life at gmail.com> wrote: