[R] change value in one cell

Jim Holtman jholtman at gmail.com
Sat Jun 11 11:05:13 CEST 2011


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:

> That's great! But, I guess I should have provided a better example for 
> my problem. The reason being that I did not consider strings. For 
> instance, in my case, I'm actually trying to add a value to a data frame 
> that looks like this:
> 
> Obs     X
> 0     NA
> 1     "01 001"
> 2     "01 002"
> 3     "01 003"
> 
> And I am actually trying to get that specific "NA" value and convert it 
> to "00 000" so it can look like this:
> 
> Obs     X
> 0     "00 000"
> 1     "01 001"
> 2     "01 002"
> 3     "01 003"
> 
> When I write the code you provided, I get this result:
> 
> x[1,"X"]<-"00 000"
> Warning message:
> In `[<-.factor`(`*tmp*`, iseq, value = c(NA, 3L, 4L, 5L, 6L, 7L,  :
> 
> invalid factor level, NAs generated
> 
> I am wondering what I am doing wrong and how to solve this problem.
> 
> Thanks for all the help!
> 
> Carlos
> 
> 
> 
> On 6/10/2011 8:34 PM, jholtman [via R] wrote:
>> you probably need to read the Introduction to R to understand indexing:
>> 
>>> x
>>  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
>>> x[4, "Y"] <- 0
>>> x
>>  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 <[hidden email] 
>> </user/SendEmail.jtp?type=node&node=3589793&i=0>> wrote:
>> 
>>> 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.
>>> 
>>> ______________________________________________
>>> [hidden email] </user/SendEmail.jtp?type=node&node=3589793&i=1> 
>> 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
>> Data Munger Guru
>> 
>> What is the problem that you are trying to solve?
>> 
>> ______________________________________________
>> [hidden email] </user/SendEmail.jtp?type=node&node=3589793&i=2> 
>> 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.
>> 
>> 
>> ------------------------------------------------------------------------
>> If you reply to this email, your message will be added to the 
>> discussion below:
>> http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3589793.html 
>> 
>> To unsubscribe from change value in one cell, click here 
>> <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3589456&code=am91cjRsaWZlQGdtYWlsLmNvbXwzNTg5NDU2fC0xNjIzNjcwNDM1>. 
>> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/change-value-in-one-cell-tp3589456p3590037.html
> Sent from the R help mailing list archive at Nabble.com.
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list